Is it possible to load image in external Storage as bitmap with android.permission.READ_MEDIA_IMAGES?

67 Views Asked by At

I am developing app with my Galaxy A31 which Android OS version is 11. My client requested to add picture for Application.

If I use READ_EXTERNAL_STORAGE permission, I can easily add this feature in the app. But Someone other developer said Google will remove entire app which have this permission until this november, And Galaxy Store also!

And the app should display the image on ImageView with bitmap.

Can I do that on Android 13+?

In my Galaxy A31, the app skip request READ_MEDIA_IMAGES!

Below is my source code. It is Java, and in onCreate()

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_MEDIA_IMAGES}, 1);
        } else {
            addPictureButton.setOnClickListener(view -> {
                Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);
                imageIntent.setType("image/*");
                startActivityForResult(imageIntent, 1);

                Uri uri = imageIntent.getData();

                String[] projection = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                photoPath = cursor.getString(columnIndex);

                cursor.close();
            });
        }

And below source code is load bitmap from external storage. I wanna know whether It cause java.lang.SecurityException.

Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
imageView.setImageBitmap(bitmap);
1

There are 1 best solutions below

3
On BEST ANSWER

For ACTION_GET_CONTENT you do not need any permission.

That was never needed.

 imageView.setImageUri(uri); 

Do away with that photo path.

For all Android versions. –