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.
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 inintent.getExtra()
, the data from the intent is returned inClipData
, which is only supported for SDK versions 18 and higher. From there, the data can be retrieved using the following code ->I've put the null check for
intent.getData()
because in case of a single image, the data is received inintent.getData()
, while in case of multiple selection, this is received asnull
.So, for sdk versions below 18, and for single selection (irrespective of sdk version), the data can be simply retrieved in the following manner :