Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider when calling image from URI in RecyclerView Adapter

Viewed 20

I am trying to load an image from URI and my code works in a activity but when I tried to load the image from an RecyclerView.Adapter in onBindViewHolder it shows me this error. And also am calling this adapter from a Fragment.

Edit:

The path of URI is an image which user picks from the device and when I have tried to load the image from different Activity not related to Activity from which image has been picked and my code works perfectly fine.

Error

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{e8f7e7f 20114:my.package.name/u0a290} (pid=20114, uid=10290) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

The code am using to load the image

user_image = findViewById(R.id.user_image);
String path = sharedPreferences.getString("imagePath", "default");
if (path.equals("default")) {
    user_image.setImageResource(R.drawable.profile);
} else {
    user_image.setImageURI(Uri.parse(path));
}

The code for image picking and saving it's path

Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"), IMAGE_REQUEST_CODE);

OnActivityResult

Uri selectedImageUri = data.getData();
SharedPreferences sharedPreferences = requireActivity().getSharedPreferences("USERDATA", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

if (selectedImageUri != null) {
    image_view.setImageURI(selectedImageUri);
    imagePath = selectedImageUri.toString();
    editor.putString("imagePath", imagePath);
    editor.apply();
 }
0 Answers
Related