7

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.

Astrum
  • 591
  • 3
  • 12
  • 25
  • You are only trying to obtain ONE uri. Well if you select multiple images then there are multiple uries. So try to grab them. Mostly that one ur is null if the user selected more than one. Please confirm. – greenapps Jan 04 '18 at 10:26
  • You should distinguish between the user being able to select multiple images. And your code to obtain all the uries. Now its unclear. Further please mention the Android version of the used devices. – greenapps Jan 04 '18 at 10:28
  • Using a better MIME type may help. There is no `)` in any MIME type. – CommonsWare Jan 04 '18 at 11:15
  • Possible duplicate of [Select multiple images from android gallery](https://stackoverflow.com/questions/19585815/select-multiple-images-from-android-gallery) – Amardeep Jan 04 '18 at 12:44

2 Answers2

15

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)
            }
        }
    }
}
Amardeep
  • 1,414
  • 11
  • 18
  • I had to rework a lot of my code to get it working, but that was pretty much the solution I was looking for. – Astrum Jan 10 '18 at 05:42
3

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.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52