javaandroidmobilephoto-picker

How to correctly collect user-selected images after partial storage access permission on Android 14?


I'm working on an Android app where users can select photos and videos from their gallery.

However, I'm facing an issue on Android 14 (API level 34) with the new partial storage access permissions. After the user grants partial access by selecting SELECT PHOTOS AND VIDEOS, Android automatically shows an image picker.

Unfortunately, I'm unable to correctly collect the images selected by the user in this scenario.

Android storage access permissions plus image picker.

Here's what I've implemented so far:

Statements

private ActivityResultLauncher<PickVisualMediaRequest> pickMedia;
private ActivityResultLauncher<String[]> permissionLauncher;
private ActivityResultLauncher<Intent> startActivityForResult;

Permission check:

public boolean checkPermission(String permission) {
    int statusCode = ContextCompat.checkSelfPermission(this, permission);
    return statusCode == PackageManager.PERMISSION_GRANTED;
}

int sdk = Build.VERSION.SDK_INT;
if (sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
    if (checkPermission(Manifest.permission.READ_MEDIA_IMAGES) || checkPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED)) {
        selectImage();
    } else {
        permissionLauncher.launch(new String[]{
                Manifest.permission.READ_MEDIA_IMAGES,
                Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED
        });
    }
}

After the permissions, I tried to do the following checks:

permissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
    boolean permissionsGranted = false;
    int sdk = Build.VERSION.SDK_INT;
    
    if (sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && (!checkPermission(Manifest.permission.READ_MEDIA_IMAGES) && checkPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED))) {
        permissionsGranted = true;
        Uri firstImage = selectFirstImageFromMedia();
        if (firstImage != null) {
            setImageInView(firstImage);
        } else {
            Toast.makeText(getApplicationContext(), "Selection canceled", Toast.LENGTH_SHORT).show();
        }
    } else if (sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && (checkPermission(Manifest.permission.READ_MEDIA_IMAGES) && checkPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED))) {
        permissionsGranted = true;
        selectImage();
    }
    
    if (!permissionsGranted) {
        Toast.makeText(this, "The app needs permission.", Toast.LENGTH_SHORT).show();
    }
});

Other logics

selectFirstImageFromMedia: (Unfortunately they do not work correctly, it seems that the photo returned is always a random one from the chosen ones.) Refs: https://developer.android.com/about/versions/14/changes/partial-photo-video-access#query-library

private Uri selectFirstImageFromMedia() {
    Uri firstImageUri = null;
    String[] projection = { MediaStore.MediaColumns._ID };

    try (Cursor cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            MediaStore.MediaColumns.DATE_ADDED + " ASC"
    )) {
        if (cursor != null && cursor.moveToFirst()) {
            long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
            firstImageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        }
    } catch (Exception e) {
        Log.e("Error retrieving image", " Details: selectFirstImageFromMedia: " + e.getMessage());
    }
    return firstImageUri;
}

selectImage

public void selectImage() {
    int sdk = Build.VERSION.SDK_INT;
    if (sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
        pickMedia.launch(new PickVisualMediaRequest.Builder()
                .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                .build());
    } else {
        Intent picIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
        picIntent.setType("image/*");
        picIntent.putExtra("return_data", true);
        startActivityForResult.launch(picIntent);
    }
}

// Logic to collect the image selected by the user on Android 14 or higher.
pickMedia = registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
    if (uri != null) {
        setImageInView(uri);
    } else {
        Toast.makeText(getApplicationContext(), "Selection canceled", Toast.LENGTH_SHORT).show();
    }
});

setImageInView

public void setImageInView(Uri uri) {
    try {
        Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
        Bitmap bMapScaled = Bitmap.createScaledBitmap(fullBitmap, ib_tirarfoto_placa.getWidth(), ib_tirarfoto_placa.getHeight(), true);
        ib_tirarfoto_placa.setImageBitmap(bMapScaled);
        checklist.setVehiclePlatePhotoFilePath(saveImage(fullBitmap));
    } catch (Exception e) {
        Log.e("Error retrieving image", "Details: setImageInView: " + e.getMessage());
    }
}

Everything works fine when the user grants full access permissions (both READ_MEDIA_IMAGES and READ_MEDIA_VISUAL_USER_SELECTED). However, when the user grants partial access, Android 14 automatically shows an image picker. In this case, I can't capture the selected image.

I'm particularly confused about handling the image selection after the picker is launched automatically by the system. I tried using selectFirstImageFromMedia() to capture the image, but it often returns a random image instead of the one the user selected.

Any help is very welcome! Thank you in advance!


Solution

  • With a little more research and with the help of @CommonsWare, I was able to understand that the automatic selector allows the user to choose one or multiple photos, so it is the developers' responsibility to manage the chosen images in the best way possible.

    In my case, I was sorting all the chosen images in ascending order based on the date they were added to the gallery, expecting that only the first one chosen by the user would be collected.

    To solve the problem, I simply added a toast that informs the user to choose only 1 image if they have chosen more than 1 in the automatic selector.

    Changed codes:

    permissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
                boolean permissionsGranted = false;
                int sdk = Build.VERSION.SDK_INT;
    
                if (sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && (!checkPermission(Manifest.permission.READ_MEDIA_IMAGES) && checkPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED))) {
                    permissionsGranted = true;
                    Uri imageFromMedia = selectImageFromMedia();
                    if (imageFromMedia != null) {
                        setImageInView(imageFromMedia);
                    }
                } else if (sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && (checkPermission(Manifest.permission.READ_MEDIA_IMAGES) && checkPermission(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED))) {
                    permissionsGranted = true;
                    selectImage();
                }
    
                if (!permissionsGranted) {
                    Toast.makeText(this, "The app needs permission.", Toast.LENGTH_SHORT).show();
                }
            });
    
    private Uri selectImageFromMedia() {
        Uri imageUri = null;
        String[] projection = { MediaStore.MediaColumns._ID };
    
        try (Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                null,
                null,
                null
        )) {
            if (cursor != null && cursor.getCount() > 1) {
                Toast.makeText(this, "Please select only one photo.", Toast.LENGTH_LONG).show();
            } else if (cursor != null && cursor.moveToFirst()) {
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
                imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
            }
        } catch (Exception e) {
            Log.e("Error retrieving image", " Details: selectImageFromMedia: " + e.getMessage());
        }
        return imageUri;
    }