The Missing Documentation: Camera Intents

Sree Raman
AndroidPub
Published in
3 min readMar 18, 2017

Whenever I follow Google’s documentation, it seems like it only gets me 90% to the solution. There always seems to be something minor that causes major problems. I was learning to Take Photos Simply and faced that exact problem.

I will just break down what you need to do step by step, so you don’t have go hunting through StackOverflow or Github Gists.

1. Create Image File

First we need to create a File that the Camera will save the image to:

  • Make sure to give your image a unique file name. I am using System.currentTimeMillis() to ensure that.
  • Notice the commented out code on Line 6&7. Use that if you want to add it to the image gallery so that other apps can access the photo taken using your app. Beginning in Android 6.0 (API level 23), you will need to ask for permissions during run time, so don’t forget to do that. You can use a library like Permiso to help you out with that.

2. Declare Permissions

Since we are saving it our app’s private directory (Line 5 above), we need to ask for WRITE_EXTERNAL_STORAGE permission for Android 4.3 and lower. Starting from Android 4.4 (SDK 19), Google decided that it was no longer required because the directory is private to your app. However, I ran into an issue where the Nexus 7 was still asking for permission even though it was running Android 5.1 (SKD 22). Google will tell you the maxSdkVersion needs to only be 18, but I had to bump it up to 22.

3. Configure the FileProvider

We need to add a FileProvider to AndroidManifest.xml so that the camera can write to the file we provide.

  • file_paths.xml goes into the xml folder in the res folder of your app (i.e. res/xml/file_paths.xml)
  • Note that if you decide different than getExternalFilesDir(Environment.DIRECTORY_PICTURES), you need to check out the documentation of FileProvider to see what you should use instead of external-path
  • Also note that what we use in android:authorities will later be used when we create the URI (see below: Line 15)

4. Create & Start the Intent

When we create the Intent, we need check to make sure that the device has a camera and is able to accept the Intent. In addition we need to add flags to grant the camera permissions to write into the file we provide.

  • Note that this String: BuildConfig.APPLICATION_ID + “.fileprovider” needs to match what we set android:authorities to in AndroidManifest.xml above.

5. Get the Activity Result

--

--