Android: how to save the camera result in a personal file

I am trying to get an image from a camera and save it directly to the application’s personal files directory. For security reasons, the image should not be publicly accessible at any time.

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

AndroidManfiest.xml

<manifest> ... <application> ... <provider android:authorities="com.my.domain" android:name="android.support.v4.content.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ... 

Res / xml / file _paths.xml

 <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="my_images" path="images/"/> </paths> 

When starting a camera operation, I perform the following of the action:

 File imageFile = getInternalImageFile(); Uri captureUri = FileProvider.getUriForFile(this, "com.my.domain", imageFile); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // grant temporary access to the file intent.setData(captureUri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // tell the camera where to save the file intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri); startActivityForResult(intent, IMAGE_CAPTURE_REQUEST_CODE); 

This causes the camera application to return immediately without any action. I suspect because he does not expect that there will be any data set in the intent (Intent.setData ()).

The above strategy does not work so well. So, how can I safely save the image taken from the camera directly to the application’s personal files directory?

+6
source share
2 answers

So, how can I safely save the image taken from the camera directly to the application’s personal files directory?

Take a picture of yourself using android.hardware.Camera . There is no guarantee that the user will have an accessible application for the camera that knows how to save data in content:// Uri . They should support them, but not everyone does. For example, the Google Camera app did not support content:// Uri for ACTION_VIDEO_CAPTURE until June 2016.

+1
source

I had the same problem, but I decided to use clipData for it.

 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setClipData(ClipData.newRawUri(null, contentUri)); intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); 

http://developer.android.com/reference/android/content/Intent.html#setClipData (android.content.ClipData)

ClipData in the intent is not used to map Intent or other similar operations. Semantically, this is similar to additional functions used to transfer additional data using Intent. The main feature of using this on additional data for data is that FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION will work with any URI elements included in the clip data. This is useful, in particular, if you want to convey an intent that contains multiple content: URIs for which the recipient may not have global permissions to access the content provider.

Hope this helps you.

Pd: Sorry for my English. :)

+9
source

Source: https://habr.com/ru/post/958344/


All Articles