title: Android : Use Camera to display image to ImageView and Save to Gallery
body: <p>I am making an app which has an option to get an image from the camera or from the gallery and display it in an ImageView.
  I got this from the <a href="http://developer.android.com/training/camera/photobasics.html" rel="nofollow">Android Developers Tutorial.</a>
  I am using a DialogInterface for this one:</p>
  
  <pre><code>private void selectImage() {
          final CharSequence[] items = { "Take Photo", "Choose from Library",
                  "Cancel" };
  
          AlertDialog.Builder builder = new AlertDialog.Builder(ReportIncident.this);
          builder.setTitle("Add Picture");
          builder.setItems(items, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int item) {
                  if (items[item].equals("Take Photo")) {
                      takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                      // Ensure that there's a camera activity to handle the intent
  
                      if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                          // Create the File where the photo should go
  
                          try {
                              photoFile = createImageFile();
                              takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  
                              startActivityForResult(takePictureIntent, 0);
  
  
                          } catch (IOException ex) {
                              // Error occurred while creating the File
                              ex.printStackTrace();
                          }
  
                      }
                  } else if (items[item].equals("Choose from Library")) {
                      Intent intent = new Intent(
                              Intent.ACTION_PICK,
                              android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                      intent.setType("image/*");
                      startActivityForResult(
                              Intent.createChooser(intent, "Select File"), 1);
                  } else if (items[item].equals("Cancel")) {
                      dialog.dismiss();
                  }
              }
          });
          builder.show();
      }
  </code></pre>
  
  <p>createImageFile creates the directory/File where the photo will be saved:</p>
  
  <pre><code>private File createImageFile() throws IOException {
      // Create an image file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      String imageFileName = "FIREFLOOD_" + timeStamp + "_";
      File storageDir = Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES);
      File image = File.createTempFile(
              imageFileName,  /* prefix */
              ".jpg",         /* suffix */
              storageDir      /* directory */
      );
  
      // Save a file: path for use with ACTION_VIEW intents
      mCurrentPhotoPath = "file:" + image.getAbsolutePath();
      return image;
  }
  </code></pre>
  
  <p>The 'Choose from Library' part is already working but I have a problem with 'Take Photo' part. Specifically on this line:</p>
  
  <pre><code>takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  galleryAddPic();
  startActivityForResult(takePictureIntent, 0);
  </code></pre>
  
  <blockquote>
    <p>Having the putExtra() and galleryAddPic() before the startActivityOnResult() 
    crashes the app after taking the picture but successfully saves the image to
    gallery. But deleting these two methods from the code displays the photo taken 
    to the ImageView reportImage.</p>
  </blockquote>
  
  <p>startActivityForResult displays the image taken to the ImageView through activityOnResult:</p>
  
  <pre><code>public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (resultCode == Activity.RESULT_OK) {
              if (requestCode == 1) {
  
                  onSelectFromGalleryResult(data);
              }else if (requestCode == 0) {
                  Bundle extras = data.getExtras();
                  Bitmap imageBitmap = (Bitmap) extras.get("data");
                  reportImage.setImageBitmap(imageBitmap);
  
  
  
  
              }
          }
      }
  </code></pre>
  
  <p>Saving the image to gallery is performed by galleryAddPic:</p>
  
  <pre><code>private void galleryAddPic() {
  
          Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
          File f = new File(mCurrentPhotoPath);
          Uri contentUri = Uri.fromFile(f);
          mediaScanIntent.setData(contentUri);
          this.sendBroadcast(mediaScanIntent);
      }
  </code></pre>
  
  <blockquote>
    <p>The question is where can I put these two methods (putExtra and
    galleryAddPic) to make these two funtions work:</p>
    
    <ol>
    <li>display the photo taken to ImageView</li>
    <li>save the photo to gallery at the same time.</li>
    </ol>
    
    <p>I can't make these two work altogether. Please help. I tried to put
    the putExtra and galleryAddPic on the onAcitivityResult but it still
    crashes. galleryAddPic won't work without the putExtra.</p>
  </blockquote>