Permission denial when using a URI in Android Studio Java?

I have successfully determined a URI with a button but when I try to set the URI to an ImageButton I get the following

Mistake:

 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 determine and save 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 { // Speichern des Bildpfads in den SharedPreferences 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 retrieve the URI:

 // Laden des Bildes aus den SharedPreferences SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); String imagePath = sharedPreferences.getString("image_button_" + tempIMGID, ""); //itemID imageBtn2.getId()

I'm asking for a solution (all permissions are actually set) I've tried a lot but now I don't know how to proceed

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JackONeill282
1 year ago

Try once

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


                                // Speichern des Bildpfads in den SharedPreferences
                                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();
                            }


                        }
                    });


    // ...


    if (!imagePath.equals("")) {
        Uri imageUri = Uri.parse(imagePath);
        try {
            InputStream inputStream = getContentResolver().openInputStream(imageUri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
      
            int width = 110;
            int height = 100;
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
      
            imageBtn2.setImageBitmap(scaledBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}