how to open gallery to select multiple image?

Prashant Sharma picture Prashant Sharma · Aug 30, 2016 · Viewed 10.3k times · Source

I want to open gallery with multiple image selection functionality and i am using following code.

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);

It opens gallery app but doesn't let me choose multiple images.

Answer

Deepak Maurya picture Deepak Maurya · Dec 19, 2019

This worked for me from api22 to api29.

Intent intent = new Intent();
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 105);

then in activity result overmethod add this code.

if (resultCode == RESULT_OK && requestCode == 105) {
            ClipData clipData = data.getClipData();
            if (clipData != null) {
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    Uri imageUri = clipData.getItemAt(i).getUri();
                    // your code for multiple image selection
                }
            } else {
              Uri uri = data.getData();
                // your codefor single image selection
            }

Note: after you got the gallery screen hold the image little longer. then in top right click "open". it will allow you to select multiple images.