Support free tutorials

vogella training Training Books

Android Camera API - Tutorial

Lars Vogel

Version 1.3


Using the Android Camera via Intent and via the API

This tutorial describes how to use the camera API in Android. The tutorial is based on Eclipse 4.2, Java 1.6 and Android 4.2.

Table of Contents

1. Android Camera
2. Android Basics
3. Tutorial: Using an Intent to make a photo
4. Tutorial: Using the camera API
5. About this website
5.1. Donate to support free tutorials
5.2. Questions and discussion
5.3. License for this tutorial and its code
6. Links and Literature
6.1. Source Code
6.2. Android Resources
6.3. vogella Resources

1. Android Camera

Most Android devices have at least one camera. Some devices have a front and a back facing camera.

Using the camera on the Android device can be done via the integration of existing camera application. In this case you would start the existing Camera application via an intent and use the return data of the application to access the result.

Alternatively you can also directly integrate the camera into your application via the Camera API.

The following description contains examples for both use cases.

2. Android Basics

The following description assumes that you have already basic knowledge in Android development.

Please check the Android development tutorial to learn the basics. Also see Android development tutorials for more information about Android development.

3. Tutorial: Using an Intent to make a photo

Create a new Android project with an activity called ImagePickActivity.

Adjust the layout of the activity to contain an ImageView with the result ID. Also add a button with the onClick property pointing to a onClick method.

Change the ImagePickActivity class to the following.



import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class ImagePickActivity extends Activity {
  private static final int REQUEST_CODE = 1;
  private Bitmap bitmap;
  private ImageView imageView;

  public void onCreate(Bundle savedInstanceState) {
    imageView = (ImageView) findViewById(;

  public void onClick(View View) {
    Intent intent = new Intent();
    startActivityForResult(intent, REQUEST_CODE);

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    InputStream stream = null;
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
      try {
        // recyle unused bitmaps
        if (bitmap != null) {
        stream = getContentResolver().openInputStream(data.getData());
        bitmap = BitmapFactory.decodeStream(stream);

      } catch (FileNotFoundException e) {
      } finally ch (IOException e) {
        if (stream != null)
          try {
          } catch (IOException e) {

4. Tutorial: Using the camera API

In this tutorial you create application which allows you to make a photo via the front camera and to save it on the SD card.


If you using the Android emulator ensure that you configured the camera and that you added an SD card during the creation of the your Android virtual device.

Create a new Android project called with an activity called MakePhotoActivity.

Add the android.permission.CAMERA permission to access your camera and the android.permission.WRITE_EXTERNAL_STORAGE to be able to write to the SD card to your AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Change the main.xml file in the res/layout folder to the following

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:layout_height="match_parent" >

        android:text="Make Photo" />


Create the following PhotoHandler class which will be responsible for saving the photo the SD card.


import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class PhotoHandler implements PictureCallback {

  private final Context context;

  public PhotoHandler(Context context) {
    this.context = context;

  public void onPictureTaken(byte[] data, Camera camera) {

    File pictureFileDir = getDir();

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

      Log.d(MakePhotoActivity.DEBUG_TAG, "Can't create directory to save image.");
      Toast.makeText(context, "Can't create directory to save image.",


    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";

    String filename = pictureFileDir.getPath() + File.separator + photoFile;

    File pictureFile = new File(filename);

    try {
      FileOutputStream fos = new FileOutputStream(pictureFile);
      Toast.makeText(context, "New Image saved:" + photoFile,
    } catch (Exception error) {
      Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename + "not saved: "
          + error.getMessage());
      Toast.makeText(context, "Image could not be saved.",

  private File getDir() {
    File sdDir = Environment
    return new File(sdDir, "CameraAPIDemo");

Change the MakePhotoActivity class to the following. This class assumes that you have a button in your layout with the onClick property pointing to a onClick() method.


import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import de.vogella.cameara.api.R;

public class MakePhotoActivity extends Activity {
  private final static String DEBUG_TAG = "MakePhotoActivity";
  private Camera camera;
  private int cameraId = 0;

  public void onCreate(Bundle savedInstanceState) {

    // do we have a camera?
    if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
    } else {
      cameraId = findFrontFacingCamera();
      if (cameraId < 0) {
        Toast.makeText(this, "No front facing camera found.",
      } else {
        camera =;

  public void onClick(View view) {
    camera.takePicture(null, null,
        new PhotoHandler(getApplicationContext()));

  private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
    return cameraId;

  protected void onPause() {
    if (camera != null) {
      camera = null;


5. About this website

5.1. Donate to support free tutorials

Please consider a contribution Support free tutorials if this article helped you. It will help to maintain our content and our Open Source activities.

5.2. Questions and discussion

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

5.3. License for this tutorial and its code

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

6. Links and Literature

6.1. Source Code

Source Code of Examples

6.3. vogella Resources

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”. The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.