Android Capture Image from Camera and Gallery With Examples

In this tutorial we’ll develop an application that picks an image from camera or gallery and display that in an ImageView.

Note: The below code works fine for pre-Android Nougat versions. For the latest working example check out the updated article.

Android Capture Image Overview

With the commence of Android Marshmallow, runtime permissions need to be implemented forefront.

Add the following permissions in the Android Manifest.xml file, above the application tag.

By adding, Play Store detects and prevents installing the application on devices with no camera.

Intent is the standard way to delegate actions to another application.
To start the native camera the Intent requires android.provider.MediaStore.ACTION_IMAGE_CAPTURE.

To choose an image from gallery, the Intent requires the following argument : Intent.ACTION_GET_CONTENT.

In this tutorial we’ll be invoking an image picker, that lets us select an image from camera or gallery and displays the image in a circular image view and a normal image view. Add the following dependency inside the build.gradle file.
compile 'de.hdodenhof:circleimageview:2.1.0'

Android Image Capture Project Structure


Android Capture Image Code

The layout for the activity_main.xml stays the same barring the icon change for the FAB button to @android:drawable/ic_menu_camera.

The content_main.xml is given below:

The code for the is given below

There are a lot of inferences to be drawn from the code above.

  • We need to ask for the Camera runtime permissions when the user starts the activity.
  • As we are starting the intent to get some result back, we need to call startActivityForResult with the relevant arguments
  • Instead of using a dialog to separately call the Intents for Camera and Gallery, we’ve used a method getPickImageChooserIntent() that creates a single chooser intent for all the camera and gallery intents(note the documents intent). Intent.EXTRA_INITIAL_INTENTS is used to add the multiple application intents at one place
  • For the camera intent, MediaStore.EXTRA_OUTPUT is passed as an extra to specify the image storage path. Without this you’ll be returned only a small resolution image.
  • The URI path for the image returned by camera is fetched inside the method getCaptureImageOutputUri().
  • The onActivityResult essentially returns a URI to the image. Some devices do return the bitmap as data.getExtras().get("data");.
  • When an image is clicked, the camera screen while returning restarts the activity thereby causing the URI stored from the method getCaptureImageOutputUri() to become null.
    Hence it’s essential that we store and restore that URI using onSaveInstanceState() and onRestoreInstanceState().
  • The bitmap is retrieved from the URI in the following line of code.
    myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri);
  • Devices like Samsung galaxy are known to capture the image in landscape orientation. Retrieving the image and displaying as it is can cause it to be displayed in the wrong orientation. Hence we’ve called the method rotateImageIfRequired(myBitmap, picUri);
  • ExifInterface is a class for reading and writing Exif tags in a JPEG file or a RAW image file.
  • In the end we call the method getResizedBitmap() to scale the bitmap by width or height(whichever is larger) and set the image to the image view using setImageBitmap.

The output of the application in action is given below. Note: To capture and display an image from camera you’ll need to run the application on a smartphone for obvious reasons.

android capture image

This brings an end to this tutorial. You can download the Android project for Image Capture from the link below.

By admin

Leave a Reply

%d bloggers like this: