Pin Me

How to Program the Google Android Camera to Take Pictures

written by: Jbeerdev•edited by: Simon Hill•updated: 8/14/2013

Learn how to program the Google Android camera to take pictures in the simplest way via the Google Android Programming Environment.

  • slide 1 of 5

    Editor's Note: This article was originally written in 2009 and has been placed in our archive. The source code is no longer able to be downloaded from the article. If you have any questions, you can reach the author at his twitter profile, @jbeerdev.

    Nowadays smartphones have a great number of hardware enhancements: camera, accelerometer, GPS, Wi-Fi... It's time to start using their whole functionality on our phones. Let's learn how to use the phone's camera. In this example, we will need to work with 2 files, the layout file and the activity file.

    Hint! → Some time ago, there was a software actualization for the 1.5 (cupcake) Android phones. It was a security improvement. One of these security changes was related to camera permission. Before this actualization, you were able to create applications that take pictures without the user's permission. Now, this issue is fixed, and if you want to use the camera in your application, you need to add the corresponding line in the AndroidManifest.xml. The line is as follows:

    (More about the AndroidManifest.xml in the article How to understand the AndroidManifest.xml file in your Android Programming Environment)

  • slide 2 of 5

    Camera Layout

    This is the easiest element to develop, well, it depends on how many improvements we want to add into our application, elements, buttons with camera functionality...we are going to do it in the simplest way. No buttons, no extra camera functionality, just take pictures. Let's have a look at the layout we are going to use.

    We can call it “camera_surface.xml"

    Hint!-> Remember! You cannot use capital letters in the resources elements, if you put “CameraSurface.xml" it will give you problems.

    Here it is:

    <LinearLayout xmlns:android=""

    android:layout_width="fill_parent" android:layout_height="fill_parent"


    <SurfaceView android:id="@+id/surface_camera"

    android:layout_width="fill_parent" android:layout_height="10dip"




    Very simple, eh? Just a LinearLayout (our container) and inside of it, a SurfaceView. SurfaceViews provides us a dedicated place to draw things on it, in this case, our camera screen.

  • slide 3 of 5

    Camera Implementation code

    Now we have to take a look at the xml code for our camera, let's have a look at the Android code. Let's create an activity called “CameraView" that implements the interface SurfaceHolder.Callback.

    public class CamaraView extends Activity implements SurfaceHolder.Callback

    This interface “SurfaceHolder.Callback" is used to receive information about changes that occur in the surface (in this case, the camera preview). SurfaceHolder.Callback implements three methods:


    This method is called when the size or the format of the surface changes.


    When, in the first instance, the surface is created, this method is called.


    This is called when the surface is destroyed.

  • slide 4 of 5

    Camera Code Continued

    Let's have a look at how to use this interface in our camera application. We'll use the onCreate method in our activity.







    mSurfaceView = (SurfaceView) findViewById(;

    mSurfaceHolder = mSurfaceView.getHolder();




    Let's comment the code lines.





    With these lines, we are telling the screen that:

    -The camera preview is going to be full-screen, without “title" (No notification bar).

    -The format of the screen allows “translucency".

    setContentView(R.layout.camera_surface );

    mSurfaceView = (SurfaceView) findViewById(;

    We set the layout to the Activity with the setContentView to the camera_surface (the one we created some lines above!) and we create a SurfaceView object, getting it from the xml file.

    mSurfaceHolder = mSurfaceView.getHolder();



    With these lines, we get the holder from the surfaceview, and we add the callback function to “this". This means that our activity is going to manage the surfaceview.

    Let's see how the callback functions are implemented.

    public void surfaceCreated(SurfaceHolder holder) {

    mCamera =;

    mCamera is an Object of the class “Camera". In the surfaceCreated we “open" the camera. This is how to start it!!

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    if (mPreviewRunning) {



    Camera.Parameters p = mCamera.getParameters();

    p.setPreviewSize(w, h);


    try {


    } catch (IOException e) {




    mPreviewRunning = true;


    This is the method that prepares the camera, sets parameters and starts the preview of the image in our Android screen. I have used a “semaphore" parameter to prevent crashing: when the mPreviewRunning is true, it means that the camera is active and has not “closed", so this way we can work with it.

    public void surfaceDestroyed(SurfaceHolder holder) {


    mPreviewRunning = false;



    This is the method where we stop the camera and release the resources associated to the camera. See, that here we set the mPreviewRunning flat to “false", with this, we prevent crashing in the surfaceChanged method. Why? Because this means that we have “closed" the camera, and we cannot set parameters or start image previews in our camera (stuff that the surfaceChanged method does) if it is closed!.

    And let's finish with the most important method:

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

    public void onPictureTaken(byte[] imageData, Camera c) {



    This method is called when a picture is taken. For example, you could create an OnClickListener on the screen that calls the PictureCallBack method when you click on the screen. This method just gives you the byte[] of the image. So, just convert it from byte[] to some image format you want, using the Bitmap and BitmapFactory class that Android offers us. This will be explained in another tutorial!!

  • slide 5 of 5


    I hope this was helpful for everyone. It should be pretty clear if you have a basic grasp of Android programming. However, maybe some concepts like “Callback", “interface" or “listener" are not clear yet. I will write a post about these “generic" words shortly to help you understand their uses more closely. Any question, just ask!!

This article has been placed in our ARCHIVE.

How-to develop Google Android Applications - Going further

Here more functionality and simple how-to guides to show you how to work with Google Android
  1. Dev Guide to Translating Apps in Android
  2. Guide to Intents in Android Development
  3. Guide to Intents for Android Developers. Part II
  4. How to Program the Google Android Camera to Take Pictures
  5. Android UI Java Programming Tutorial