In trying to select an image from the android file system, I am currently using the following code:
public void getPhotoFromSystem(View v) //implement selecting multiple files
{
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*)");
startActivityForResult(intent, READ_REQUEST_CODE);
}
Followed by a method such as:
public void onActivityResult(int requestCode, int resultCode, Intent resultData)
{
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK){
uri = resultData.getData();
//do some more stuff
}
This works, but doesn't actually allow me to select multiple files at once, and also won't allow me to grab a photo outside the default photo gallery. I've tried some other suggestions I've seen, and nothing has worked.
What i understood from you problem is that you require mulitple image selection in one go.
Note that Android's chooser has Photos and Gallery available on some devices. Photos allows multiple images to be selected. Gallery allows just one at a time.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*"); //allows any image file type. Change * to specific extension to limit it
//**These following line is the important one!
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURES); //SELECT_PICTURES is simply a global int used to check the calling intent in onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SELECT_PICTURES) {
if(resultCode == Activity.RESULT_OK) {
if(data.getClipData() != null) {
int count = data.getClipData().getItemCount();
int currentItem = 0;
while(currentItem < count) {
Uri imageUri = data.getClipData().getItemAt(currentItem).getUri();
//do something with the image (save it to some directory or whatever you need to do with it here)
currentItem = currentItem + 1;
}
} else if(data.getData() != null) {
String imagePath = data.getData().getPath();
//do something with the image (save it to some directory or whatever you need to do with it here)
}
}
}
}
when we select multiple files the result come under ClipData so we need to get the ClipData from the data and then traverse other them to get the number of Uri.
if (data.getClipData()!=null){
//multiple data received
ClipData clipData = data.getClipData();
for (int count =0; count<clipData.getItemCount(); count++){
Uri uri = clipData.getItemAt(count).getUri());
//do something
}
}
Note: This will work for API level 16 and above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With