title: Android - How to take a picture from camera or gallery
body: <p>I would like to ask the user to take a picture, either from camera or from the existing pictures in the device (gallery or whatever). How can I do that?</p>
  
  <p>I have implemented the solution below, which seems to work fine, but the documentation is quite confusing so I would like to know if there are better solutions.</p>
  
  <p>Also, check out this related post. There you will see how to get the image path or Bitmap: <a href="https://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically">Get/pick an image from Android&#39;s built-in Gallery app programmatically</a></p>
  
  <p>So, in my solution you would create a <code>TakePictureHelper</code> object and do the following.</p>
  
  <p>Let's say you display a dialog where the user can choose "camera" or "other". When the user chooses an option you would call either <code>takeFromCamera()</code> or <code>takeFromOther()</code>. When the picture is taken (or not) the <code>onActivityResult()</code> method will be called. There you would call <code>retrievePicture</code>, which will return the Uri of the picture or null if no picture was taken.</p>
  
  <p>Please let me know what you think, share ideas or ask me anything if I wasn't clear.</p>
  
  <p>Thank you very much!</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Environment;
  import android.provider.MediaStore;
  import java.io.File;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  public class TakePictureHelper {
  
      public final static int REQUEST_CAMERA = 1;
      public final static int REQUEST_OTHER = 2;
  
      private Uri cameraImageUri;
  
      /**
       * Request picture from camera using the given title
       */
      public void takeFromCamera(Activity activity, String title)
      {
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          File cameraImageOutputFile = new File(
                  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                  createCameraImageFileName());
          cameraImageUri = Uri.fromFile(cameraImageOutputFile);
          intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
          activity.startActivityForResult(Intent.createChooser(intent, title), REQUEST_CAMERA);
      }
  
      /**
       * Request picture from any app (gallery or whatever) using the given title
       */
      public void takeFromOther(Activity activity, String title)
      {
          Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
          intent.setType("image/*");
          activity.startActivityForResult(Intent.createChooser(intent, title), REQUEST_OTHER);
      }
  
      /**
       * Retrieve the picture, taken from camera or gallery.
       *
       * @return the picture Uri, or null if no picture was taken.
       */
      public Uri retrievePicture(Activity activity, int requestCode, int resultCode, Intent data)
      {
          Uri result = null;
  
          if (resultCode == Activity.RESULT_OK) {
  
              if (requestCode == REQUEST_OTHER) {
                  result = data.getData();
              } else if (requestCode == REQUEST_CAMERA) {
                  result = cameraImageUri;
              }
          }
  
          return result;
      }
  
      private String createCameraImageFileName() {
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
          return timeStamp + ".jpg";
      }
  }
  </code></pre>