How can I fix the 'Permission Denial' error when setting a URI on an ImageButton in Android Studio using Java?

25 Views Asked by At

I have successfully obtained a URI with a button, but when I try to set the URI on an ImageButton, I get the following error:

java.lang.SecurityException: Permission Denial: opening provider com.miui.gallery.provider. GalleryOpenProvider from ProcessRecord{54eddcf 26809:com.example.sortex/u0a568} (pid=26809, uid=10568) that is not exported from UID 10133

My code with the error:

if (!imagePath.equals("")) {
    Uri imageUri = Uri.parse(imagePath);
    try {
      Bitmap bitmap = uriToBitmap(ItemActivity.this, imageUri);
      
      int width = 110;
      int height = 100;
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
      
      imageBtn2.setImageBitmap(scaledBitmap);

Code to retrieve and store the URI:

public class ItemActivity extends AppCompatActivity {
    private ActivityResultLauncher<Intent> galleryLauncher =
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                    result -> {
                        if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                            Uri imageUri = result.getData().getData();
                            try {

                                SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("image_button_" + tempIMGID, imageUri.toString()); // itemID imageBtn2.getId()
                                editor.apply();

                                finish();
                                startActivity(getIntent());

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    });

Code to invoke the URI:

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
String imagePath = sharedPreferences.getString("image_button_" + tempIMGID, "");
imageBtn2.getId()

(all permissions are actually set). I have tried a lot of things already, but now I'm at a loss and don't know what to do next.

I tried to retrieve a URI using an Intent (works) and then display this URI on an ImageButton (doesn't work).

0

There are 0 best solutions below