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.

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:

  • If the user only gave partial access:
    READ_MEDIA_IMAGES would be false and READ_MEDIA_VISUAL_USER_SELECTED would be true, then I would search for the photo he selected

  • If the user accepted all permissions:
    Both READ_MEDIA_IMAGES and READ_MEDIA_VISUAL_USER_SELECTED would be true, so I would show the image selector to him, since in these cases Android 14 does not show it automatically.

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!

4

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;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật