Android Camera API Tutorial


In Android Camera API Tutorial, I have covered how to use  camera in Android applications. We are chucking out general code snippets like declaring and initializing fields, only important parts are explained here. For full source code you can download project at the end of this tutorial.


To interact with camera, we need to add few permissions to manifest file.


Check if camera available:

We need be sure that device has camera before using camera specific code. We have already added tag in manifest which restricts Google Play from installing that application on devices which do not have that feature.

There are ways of installing application other than Google Play, so you can check for camera with the help of code too.

private boolean isCameraAvailable() {

if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) 

// this device has a camera
return true;

} else 

// no camera on this device
return false;



Take a picture:

We will now take a picture with the help of INTENT.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// create a file to save image
File fileUri = getOutputMediaFileUri(1);

// specifying path to save image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// starting the image capture Intent
startActivityForResult(intent, 100);


Receive the picture taken:

Since, we have called startActivityForResult() so we should be expecting onActivityResult() being called after camera app has closed. There might be two cases, either an image was captured or camera was cancelled.


protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// check if its for capturing image only


if (resultCode == RESULT_OK) {

// successfully captured the image
// display it in image view


} else if (resultCode == RESULT_CANCELED) {

// user cancelled Image capture
Toast.makeText(getApplicationContext(),"User cancelled image capture", Toast.LENGTH_SHORT).show();

} else {

// failed to capture image

Toast.makeText(getApplicationContext(),"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();





Preview Captured Image:

Now that we have received an image, now we will display it in an imageview.

void displayCapturedImage() {

try {

// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();

// downsizing image as it throws OutOfMemory Exception for larger images
options.inSampleSize = 8;

final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);



catch (NullPointerException e) {




Reference: Android Documentation