Camera / Picture Sample App


Cristiano Ferreira

This sample shows how to use the Android* picture API. The application takes a burst of n photos, where n can be set in the capture function. This burst feature is exclusive to Intel hardware.


The class itself needs to extend the activity class, which is basically an objective that summons a window for you with a single call. For now, it is just a blank window, but we will set what the user will see in the following method.


The activity contains an onCreate() method that is used for initialization. You can specify a window you would like to use via the setContentView() method. This sample in particular uses R.layout.camera_picture. This is a premade UI specifically for taking pictures with the camera. In the sample we also set the SurfaceView to be the “preview” view. To summon the preview, we need to specify a callback for the preview holder.


The onResume() function basically specifies what happens when the window is the object of focus. Whatever is specified in this function persists until onPause() is called. First we must call super.onResume() to do general built-in activity requirements. In the sample we use this function to get information about all of the cameras on the device, and then we start the camera. We specify to use the back facing camera first in this example.


This function specifies what actions we want to take when the activity is no longer the center of attention. Since there is no longer a point in displaying a preview, we stop it and then release the camera reference. You must call super.onPause() at the end for general activity housekeeping.


This function checks if the preview view is activated, If so, it calls the photoCallback function, which takes the picture.

getBestPreviewSize(int, int, Camera.Parameters)

Finds the highest supported preview size supported on the device.


Gets the smallest picture size supported on the device.


This is the specified callback function for creating the preview within the onCreate() function. You pass it the SurfaceHolder object you selected, and the function assigns it to the camera.PreviewDisplay property.


This is where the surface holder is actually changed to the preview, and the inPreview flag is switched to true.


A required method, but nothing needs to be released here.


This is the callback function specified for the Camera.PictureCallback property. This starts the SavePhotoTask method, and sends it the data for the captured photo.


The SavePhotoTask class extends the AsyncTask class, which is basically a way to start a background task. This is used for operations that may take longer than expected so that the user experience doesn’t suffer. Since we don’t know exactly how long the photo saving task will take, we do it here. We need to create a file and assign an output stream to put the information at the specified file location.

Basic Lifecycle

  • Photo activity is called
    • onCreate() is called
      • Sets the view to camera_picture layout.
      • Sets SurfaceView callback to surfaceCallback() to get a preview screen.
        • SurfaceCallback() sets camera.previewDisplay to the previewHolder variable we specified.
        • Calls surfaceChanged() to set the preview size and the smallestpicturesize by calling getBestPreviewSize and getSmallestPictureSize.
        • Sets up the preview window with all of the parameters specified.
    • onResume() is called
      • Enumerates cameras, looks for the back facing camera and then opens it.
    • Preview screen is being displayed and is waiting on user interaction.
    • When user presses capture, onOptionsItemSelected() is called.
      • If the function is called while preview is being displayed, the camera.takePicture method calls photoCallback.
        • PhotoCallback calls SavePhotoTask()
          • SavePhotoTask stores the photo in the users external storage directory as a jpeg.

About Author

Cristiano was an intern with Intel from January-August 2012. During his internship he supported his team with the validation of the windows* 8 tablet platform. He also developed several sample windows* 8 applications. Cristiano has returned to school as a full time student to complete his bachelor's degree in computer science. He took up a full time position with Intel and will be joining soon after completing his degree.

For more complete information about compiler optimizations, see our Optimization Notice.