Fixing SecurityException requiring a valid ContentProvider on Android 8
Egemen Hamutçu
19910

Hi Egemen Hamutçu, I implemented this as you explained by creating a class that extends content provider. But when I ‘m trying to load the uri into an image view, I keep getting the below error.

java.io.FileNotFoundException: com.test.PhotoProvider/data/user/0/com.test/cache/cropped953508219.jpg: open failed: ENOENT (No such file or directory)

This is happening even on < 8.0 version.

Here is how I defined the provider in manifest file.

<provider
android:name="com.test.PhotoProvider"
android:authorities="${applicationId}.PhotoProvider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
/>
</provider>

provider paths has the below line

<external-path name="/storage/emulated/0" path="/" />

I’m loading the uri using below code

Uri decodedImgUri = PhotoProvider.getPhotoUri(new File(imgPath));
ContentResolver cr = activity.getContentResolver();
cr.notifyChange(decodedImgUri, null);
ImageLoader.getInstance().displayImage(decodedImgUri.toString(), holder.image);
public static Uri getPhotoUri(File file) {
Uri outputUri = Uri.fromFile(file);
Uri.Builder builder = new Uri.Builder()
.authority(CONTENT_PROVIDER_AUTHORITY)
.scheme("file")
.path(outputUri.getPath())
.query(outputUri.getQuery())
.fragment(outputUri.getFragment());

return builder.build();
}