I am trying to allow select multiple images in my android application which works fine but for some reasons i am not able to get the data of the images selected in the activity result. The clipData seems to be empty when i select multiple images but the data works fine when i select a single image. Here is the code i currently have.
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
Toast.makeText(getApplicationContext(), ""+mClipData.getItemCount(), Toast.LENGTH_SHORT).show();
} else if (data.getData() != null) {
Toast.makeText(getApplicationContext(), "One Image", Toast.LENGTH_SHORT).show();
}
}
}
For some reasons when i select one image i get the toast One Image but when i select multiple images no toast whereas i am expecting the clipdata to show number of items(images) selected.
2
Answers
You should remove check "data.getData() != null", hope it’s ok
Kotlin
Try this code for multiple image selection. You can use Image Uri and Image Path it’s up to your need is it path or uri.