Home » Android » android – onActivityResult getting RESULT_CANCELLED when using Intent.EXTRA_ALLOW_MULTIPLE

android – onActivityResult getting RESULT_CANCELLED when using Intent.EXTRA_ALLOW_MULTIPLE

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have the following button in my activity, that opens the gallery to select single or multiple images, and below this, the OnActivityResult function, that is returning result as RESULT_CANCELLED for multiple images, and RESULT_OK for a single image. Not sure why it’s happening. Can someone please help.

buttonGallery.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), choose_picture);
    //startActivity(intent);
    }
});

//OnActivityResult for the above

public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(requestCode == choose_picture) {
          Uri imageUri = (Uri)data.getParcelableExtra(Intent.EXTRA_STREAM);
      //Do something
}

I’m getting data.getData() as null, data.getExtras() as null.

Can someone guide me how to get the required results from the above code. I want the URIs of all images that the user selects from the gallery.

PS : It’s working fine for a single image, not sure why.

How to&Answers:

Finally I got the solution to this. When using EXTRA_ALLOW_MULTIPLE, when there is more than one content that the user is selecting, instead of being returned in intent.getExtra(), the data from the intent is returned in ClipData, which is only supported for SDK versions 18 and higher. From there, the data can be retrieved using the following code ->

 if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) 
                                            && (null == data.getData())) 
 {
                ClipData clipdata = data.getClipData();
                for (int i=0; i<clipdata.getItemCount();i++)
                {
                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), clipdata.getItemAt(i).getUri());
                        //DO something
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
}

I’ve put the null check for intent.getData() because in case of a single image, the data is received in intent.getData(), while in case of multiple selection, this is received as null.

So, for sdk versions below 18, and for single selection (irrespective of sdk version), the data can be simply retrieved in the following manner :

InputStream ist = this.getContentResolver()
                            .openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(ist);