How to fix FileUriExposedException
Add provider element to AndroidManifest.
<application
....
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.devez.app.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Add ##xml file
Add the xml folder to the res folder of the project and add the filepaths.xml file.
It should be the same as the file name specified in the provider element of the AndroidMeifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="files" path="." />
</paths>
Create a Uri using the FileProvider.getUriForFile method.
As the second parameter, use the android:authorities value specified in AndroidManifest.
private Uri createImageCaptureUri(){
ContextWrapper cw = new ContextWrapper(this);
String imgName = "img_" + System.currentTimeMillis() + ".jpg";
File file = new File(cw.getFilesDir(), imgName);
Uri result;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
result = FileProvider.getUriForFile(this, "com.devez.app.fileprovider", file);
} else {
result = Uri.fromFile(file);
}
return result;
}
Note that the file folder must be created in the path specified in filepaths.xml.
Because the name is specified as files in the filepaths.xml file, the folder of the file must use the getFilesDir method.
If a file is created in another folder, the following exception is thrown.
java.lang.IllegalArgumentException: Failed to find configured root that contains
Android Developer File Sharing Settings