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);
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?
source share