Android camera example

In this Android tip, I am going to provide you an example of integrating Camera to your application so the user can take, view, and save picture to a file.
In Android, there are two different methods to integrate Camera to your application. One method uses the built-in Camera application. Another method uses Camera API. In this tip, the latter will be implemented.

To integrate Camera in your application using Camera API, in your code, you need to create an instance of Camera class using its static method open() or open(int CameraID). Some Android phones (e.g. The Galaxy Grand Max, Galaxy A5) have two cameras: front-facing and back cameras. With the open (int CameraID) method, you can specify the camera to be used. Typically before taking a picture, camera allows to preview the picture. You will create a class that extends the SurfaceView class. An instance of the class is used to preview the picture. To take the picture, you simply call the takePicture (Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg) method of the Camera object. It takes three parameters. The first parameter signals the motion of picture capture. The second and third parameters are Callback interface used to supply image data from a photo capture. The first and second parameters can be null.
Now to have an example application on using Camera API, you create a new Android project. Then, in the activity_main.xml file, you add a FrameLayout, a Button, and an ImageView.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=""

    android:orientation="vertical" >
        android:layout_height="199dp" />

        android:text="Take picture" />

        android:scaleType="fitXY" />


Here is the code written in the MainActivity class to use Camera API to allow the user to preview, take, and save a picture to a file. After the first picture capture, the camera is reset to allow you to take another picture.

public class MainActivity extends Activity {

  private Camera cameraObject;
  private CameraView cameraView;
  private ImageView pic;
  private Handler handler;
  FrameLayout preview;
  protected void onCreate(Bundle savedInstanceState) {
    pic = (ImageView)findViewById(;
    handler=new Handler();
  private Camera getCamera() {
     int cameraId = -1;
     Camera object = null;
     // Search for front or back 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 || info.facing==CameraInfo.CAMERA_FACING_BACK) {
       cameraId = i;
     return object;

  // Callback interface used to supply image data from a photo capture.
  private PictureCallback PCallback= new PictureCallback() {

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

    Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
      Toast.makeText(getApplicationContext(), "Photo not taken", Toast.LENGTH_SHORT).show();
   // Show image
   // Save to file  
   FileOutputStream outStream = null;
       try {
           outStream = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+System.currentTimeMillis()+".jpg");
       } catch (FileNotFoundException e) {
       } catch (IOException e) {

    // Release camera
    // Reset camera after a short time period
    handler.postDelayed(new Runnable(){
   public void run(){
   // reset camera and preview
    }, 300);
  // Get camera object and prepare preview
  public void initCamera(){
   cameraObject = getCamera();
    cameraView = new CameraView(this, cameraObject);
    preview = (FrameLayout) findViewById(;
  public void takePhoto(View view){
    cameraObject.takePicture(null, null, PCallback);
  protected void onResume(){
  public void releaseCamera(){
  // Release camera and remove surface view from the preview
  // Camera preview
  private class CameraView extends SurfaceView implements SurfaceHolder.Callback {

  private SurfaceHolder surfaceHolder;
  private Camera camera;

  public CameraView(Context context,Camera camera) {
    super(context); = camera;
    surfaceHolder = getHolder();

  public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

  public void surfaceCreated(SurfaceHolder holder) {
    try  {
   if (Build.VERSION.SDK_INT >= 8) {
     } catch (IOException e) {

  public void surfaceDestroyed(SurfaceHolder arg0) {


  protected void onPause(){

Before you run the example application, do not forget to add the following lines of code to AndroidManifest.xml file.

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


Posted by: Dara | post date: 01-20-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.