Blog

Android Series: Taking photos with Android built in camera

Today I will describe how to take a photo in android application using the built in camera. The way to do it is to simply start ‘activity for result’ instead of simple ‘activity’ and specify android camera application as our activity.
Then, once a picture is taken with a nice built in camera application, we can get it back in our app for further processing:

Main activity class:

package com.softwarepassion.androcamera;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroCamera extends Activity {
    private static final int IMAGE_CAPTURE = 0;
    private Button startBtn;
    private Uri imageUri;
    private ImageView imageView;

    /** Called when the activity is first created.
     *  sets the content and gets the references to
     *  the basic widgets on the screen like
     *  {@code Button} or {@link ImageView}
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView)findViewById(R.id.img);
        startBtn = (Button) findViewById(R.id.startBtn);
        startBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startCamera();
            }
        });
    }

    public void startCamera() {
        Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
        String fileName = "testphoto.jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        values.put(MediaStore.Images.Media.DESCRIPTION,
                "Image capture by camera");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        imageUri = getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent, IMAGE_CAPTURE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_CAPTURE) {
            if (resultCode == RESULT_OK){
                Log.d("ANDRO_CAMERA","Picture taken!!!");
                imageView.setImageURI(imageUri);
            }
        }
    }
}

and layout xml (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<Button android:text="Start Camera"
    android:id="@+id/startBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</Button>
<ImageView android:id="@+id/img"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</ImageView>
</LinearLayout>

10 responses on “Android Series: Taking photos with Android built in camera

  1. Scott March 10, 2011 at 10:56 pm

    Nice article bro. Is there any way to prevent the image from also being written to the SD card? I want to take the file from the camera and head off to my server with it, yet a duplicate shows on my phone.

    Peace,
    Scott

  2. mohammad shankayi March 22, 2011 at 4:12 am

    hi, really thanks bro

  3. Kutbi September 19, 2011 at 11:09 am

    thanks..
    this works good but when i set camera image on imageview it rotating from portrait to landscape ..means image is not set proper which i captured from camera…do u have any solution regarding this

  4. bamini January 31, 2012 at 8:34 am

    hi, am getting exception unsupported operation execution,unknown uri

  5. soft_developer March 27, 2012 at 11:30 am

    Hi, thanks for this tutorial, but onActivityResult the application pass directly to RESULT_CANCELED and my intent is NULL, how can I fix this problem please?
    Thanks

  6. Deni May 29, 2012 at 1:00 pm

    Hi, can you plz write me a code that would after turning camera on shout photo automaticly and save it and go back to app ? I would be really thankful !

  7. admin May 29, 2012 at 1:12 pm

    sorry, no time

  8. Jigar July 16, 2012 at 2:57 pm

    really good job.. thanks buddy.. 🙂

  9. Val October 18, 2012 at 3:47 pm

    Hey guys, it’s written “Andorid” on the title 😉 Bye

  10. Ganesh G March 8, 2013 at 10:08 am

    Very Nice example.its work for me fine.in that i want to store this image into database and retrieve.
    Thanks.

Leave a Reply