It is exposed outside the application through ClipData.Item.getUri

I try to fix the problem after adding a new function to the Android file system, but I get this error:

android.os.FileUriExposedException: file:///storage/emulated/0/MyApp/Camera_20180105_172234.jpg exposed beyond app through ClipData.Item.getUri() 

I hope someone helps me fix this :)

thanks

 private Uri getTempUri() { // Create an image file name SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String dt = sdf.format(new Date()); imageFile = null; imageFile = new File(Environment.getExternalStorageDirectory() + "/MyApp/", "Camera_" + dt + ".jpg"); AppLog.Log( TAG, "New Camera Image Path:- " + Environment.getExternalStorageDirectory() + "/MyApp/" + "Camera_" + dt + ".jpg"); File file = new File(Environment.getExternalStorageDirectory() + "/MyApp"); if (!file.exists()) { file.mkdir(); } imagePath = Environment.getExternalStorageDirectory() + "/MyApp/" + "Camera_" + dt + ".jpg"; imageUri = Uri.fromFile(imageFile); return imageUri; } 
+60
source share
2 answers

Add the following code block before starting the camera or viewing the file

  StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); 

Please familiarize yourself with the strict mode of the referral link and explain all its use and technical details.

+106
source

For SDK 24 and above, if you need to get a Uri file outside the repository of your application, you have this error.
The @ eranda.del solutions allow you to change the policy to allow this, and it works fine.

However, if you want to follow Google’s rules without changing the API policy of your application, you must use FileProvider.

First, to get the file URI, you need to use the FileProvider.getUriForFile () method:

 Uri imageUri = FileProvider.getUriForFile( MainActivity.this, "com.example.homefolder.example.provider", //(use your app signature + ".provider" ) imageFile); 

Then you need to configure the provider in your Android manifest:

 <application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.homefolder.example.provider" android:exported="false" android:grantUriPermissions="true"> <!-- ressource file to create --> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"> </meta-data> </provider> </application> 

(In "permissions" use the same value as the second argument to getUriForFile () method (application signature + ".provider"))

And finally, you need to create a resource file: "file_paths". This file must be created in the res / xml directory (you probably need to create this directory as well):

 <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="." /> </paths> 
+88
source

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


All Articles