you're reading...
HCI, Tech

Four Steps to Start Using Camera in Your Android Application

As a hello world of Android camera, our goal is, with a minimum of code, to show the camera image on the screen (similar to the camera app). After creating a normal Android project:

1. Add user permission to the manifest xml file

This line is added as a node of <manifest>:

<uses-permission android:name="android.permission.CAMERA"/>

2. Create and initialize a Camera object

private Camera mCamera;

// ...

mCamera = Camera.open();

3. Add a private CameraView class extending SurfaceView to display images captured from the camera

Detailed code see below. Whenever you want non-static view (animation, camera, etc.) you might need to extend the SurfaceView.

4. Create and initialize a CameraView object and setContentView it

private CameraView mView;

// ...

mView = new CameraView(this);

Sample Code:

package me.xiangchen.hellostuff;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class HelloCameraActivity extends Activity {

private Camera mCamera;
 private CameraView mView;

 public void onCreate(Bundle savedInstanceState) {

 mCamera = Camera.open();
 mView = new CameraView(this);


 // extending SurfaceView to render the camera images
 private class CameraView extends SurfaceView implements SurfaceHolder.Callback{
 private SurfaceHolder mHolder;

 public CameraView(Context context) {

 mHolder = this.getHolder();



 public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
 public void surfaceCreated(SurfaceHolder holder) {

 try {
 } catch (IOException e) {

 public void surfaceDestroyed(SurfaceHolder holder) {






About Xiang 'Anthony' Chen

Making an Impact in Your Life


2 thoughts on “Four Steps to Start Using Camera in Your Android Application

  1. Hey, your tutorial very useful but it can’t work for me. when i run with my device(or emulator) it display “the application has stopped unexpectedly” if your code has updated please tell me. sorry for my bad english. thanks.

    Posted by jannii | November 7, 2012, 6:33 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: