Search code examples
androidimageandroid-intentandroid-gallery

Picking multiple images on clicking from gallery


I am new to android and I have written code to select multiple images from gallery and for achieving this i have written 2 ways of doing this. On onActivityResult I am using data.getClipdata to recieve each image.

This is the first way of selecting multiple images.

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"), RESULT_LOAD_IMAGE);

This is the second way of selecting multiple images.

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent,RESULT_LOAD_IMAGE);

I went with the second way of selecting multiple images, because on implementing this it looked better for my app and I could select multiple images on just click and stopped using first way as I have to select multiple images using long press. Thing were going fine as on android studio emulator and Nokia device USB debugging. But on one Samsung device I was not able to select images at all and here the first method worked. So my question is how to achieve multiple images in this scenario using second way if possible and will this issue on some other device too?


Solution

  • As you mention in question for Samsung option 1 and for other device option 2 work. so just put condition and check which device is use at that time

    String deviceName = "Samsung";
    if(deviceName.e(android.os.Build.MODEL)){
        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"), RESULT_LOAD_IMAGE);
    }else{
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }