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",
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"> <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>
source share