Nathan Taylor Nathan Taylor - 4 months ago 21
Android Question

Android: How to save the camera result to a private file

I am trying to get an image from the camera and save it directly to my app's private files directory. For security concerns, the image should not be publicly accessible at any time.

In general, the way you grant temporary access to a private file is to use a ContentProvider and set the GRANT_WRITE_URI_PERMISSION flag in the Intent. Following the documentation in FileProvider, I have done the following:


android:resource="@xml/file_paths" />


<paths xmlns:android="">
<files-path name="my_images" path="images/"/>

When launching the camera activity, I execute the following from an activity:

File imageFile = getInternalImageFile();
Uri captureUri = FileProvider.getUriForFile(this, "", imageFile);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// grant temporary access to the file

// tell the camera where to save the file
intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);

startActivityForResult(intent, IMAGE_CAPTURE_REQUEST_CODE);

This however results in the camera app returning immediately without doing anything. I suspect because it's not expecting there to be any data set on intent (Intent.setData()).

The above strategy isn't working out so well. So, how can I securely save an image captured from the camera directly to my app's private files directory?


So, how can I securely save an image captured from the camera directly to my app's private files directory?

Take the picture yourself, using android.hardware.Camera. There is no guarantee that the user will have a camera app available that knows how to save data to content:// Uri paths. They are supposed to support them, but not all do. For example, the Google Camera app did not support a content:// Uri for ACTION_VIDEO_CAPTURE up through June 2016.