Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getClipData() returning null when a single image is selected when working fine when multiple images are selected

Tags:

android

Basically, I need to select multiple images. My code is working fine when I select multiple images but it's not working when I select a single image.

I am using this code to select the images

public void getPhotoFromGallery(){
    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);
}

My onActivityResult() is

super.onActivityResult(requestCode, resultCode, data);
    Log.i("onActivityResult: ", "STARTED");
    if(requestCode == 1 && resultCode == RESULT_OK && data!=null){
        try {
            Log.i("Error: ", String.valueOf(data));
            ClipData mClipData = data.getClipData();
            Log.i("Error: ", String.valueOf(mClipData));
            .....

            .....
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

When I select multiple images, My code is working fine. But when I select a single image, data.getClipData() is returning null. I basically need a list of URI of all the select images. I don't understand the problem. Also, String.valueof(data) shows there is URI present when one image is selected but data.getClipData() still returning null.

like image 253
Archaic Avatar asked Sep 05 '25 03:09

Archaic


1 Answers

Also, String.valueof(data) shows there is URI present when one image is selected but data.getClipData() still returning null

You will get that Uri from calling data.getData(), where data is the Uri delivered to onActivityResult().

like image 159
CommonsWare Avatar answered Sep 08 '25 00:09

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!