Free Android app covering all aspects of addiction, including prevention and treatment

Using Android's camera in your application

  • Written by  Clive

Taking photos from within your app using the device's camera app

If you need to take a photo from within your app, just tap into the Camera app to do it for you.

Here’s an example.

Use the device's camera app to take the photo

Say I need to take a photo from within my CameraActivity and save it so that it is accessible from another app. All I do is supply details of where I want the photo saved and then ask the camera app to do the rest.

Tell the camera to save the photo in a public folder

Photos accessible by other apps should be saved in the public Pictures directory.  You can get this directory by calling getExternalStoragePublicDirectory(), passing DIRECTORY_PICTURES as a parameter.

Note the following:

  • Environment – accesses the environment variables
  • pictureFileName – is my file name for the photo

Camera app create directory

I also set the write permission in the manifest file like this:

Access the camera to take the photo

Call startActivityForResult(), passing it the intent to start the camera. The camera app then takes over and takes the picture. It automatically saves the full sized photo in the supplied directory.

Note the following:

  • MediaStore – a content provider, it manages the database of all media files
  • ACTION_IMAGE_CAPTURE – Intent action to get the camera to take a photo and return it
  • resolveActivity() - checks for an activity that matches my intent, i.e. one that can take a picture. If it can’t find one then it can’t continue
  • EXTRA_OUTPUT – contains the Uri of the file that I want to save
  • TAKE_PICTURE – my requestCode to identify the result returned by startActivityForResult()

Start camera activity

Get the picture

On successfully taking the photo, the camera app sends a result back to my app. The result includes a thumbnail. It would also have saved a full sized photo but only because I supplied details of where to save it.

The onActivityForResult() method checks the result for a thumbnail and sends it to the DisplayBitmapActivy.

Note the following:

  • requestCode  == TAKE_PICTURE  – this is the unique code identifying this particular result which matches my request code
  • resultCode  == RESULT_OK - the camera operation was successful
  • data – this is a returned intent containing the thumbnail

Camera activity result

Display the picture

The DisplayBitmapActivity displays the photo.

It looks for the file and if it exists, resizes and displays it. If not then it checks for a thumbnail and displays that. If there’s no thumbnail then it displays the default image.

Get the file

Note the following:

  • decodeResizedBitmapFromSource() – this method resizes the image to the size passed to it as parameters. It considerably reduces the file size making swopping images quicker and less demanding on memeory.

Decode saved bitmap

If the camera succeeded in taking the photo, it would have saved a full sized photo. The image could be quite large. Since I don’t need to process such a big photo, I can reduce its size, freeing memory.

Note that the original saved file remains unaltered. It is only the image that I want to display that will be reduced in size. This process is useful for swopping images.

Reduce the image to free up memory

The decodeResizedBitmapFromSource() method receives, the path to the file and the required dimensions of the new image, as parameters. It returns a resized, smaller image.

Decoding the file

Note the following:

  • inJustDecodeBounds – initially we set this to true so that we can query the bitmap to get its size without having to create a bitmap that would occupy memory
  • inSampleSize – sets the options to notify the decoder to return a smaller image based on this sample size. The sample size is the number of pixels corresponding to one pixel in the decoded image. For example an inSampleSize of 4 will return an image ¼ the original size
  • calculateInSampleSize() – returns the inSampleSize. More on this later
  • inJustDecodeBounds – the second time this is called, it is set to false. The decoder will now return the resized bitmap based on inSampleSize

Decode bitmap sample size

Getting the sample size

Note the following:

  • We get the original dimensions out of the bitmap factory options passed as a parameter
  • We also have dimensions for the new, resized image that are also passed as parameters
  • We then calculate the inSampleSize ratio and return this as in int

Bitmap inSampleSize

Get the project file

Download the Android Studio project file Download icon