7

the describe: http://www.rqgg.net/topic/vrvkz-select-multiple-images-from-android-gallery.html

If the caller can handle multiple returned items (the user performing multiple selection), then it can specify EXTRA_ALLOW_MULTIPLE to indicate this.

This is pretty interesting. Here they are referring it to the use case where a user can select multiple items?

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void selectPhotos(){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(Intent.createChooser(intent,
                "select multiple images"), SELECT_PHOTOS_RESULT);
    }


protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        super.onActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {

            switch (requestCode) {
                case SELECT_PHOTOS_RESULT:
                    //how to get the Uris?
                    ...
                    break;

        }

    }
user
  • 86,916
  • 18
  • 197
  • 190
qinmiao
  • 5,559
  • 5
  • 36
  • 39
  • All images in the Gallery are in the MediaStore which can be pulled from the ContentProvider. http://stackoverflow.com/questions/3680357/how-to-query-android-mediastore-content-provider-avoiding-orphaned-images – DeeV Jan 12 '14 at 03:53
  • Thank you, but: i want to get user selected images from onActivityResult(),don't get all images. – qinmiao Jan 12 '14 at 04:00
  • 1
    Oh I see what you're doing. This is relatively knew EXTRA, so I don't know the answer. Although, from the docs, it seems the data is in the resulting `ClipData` when you call `getClipData()` on the `Intent`. *My guess* is you can call `ClipData#getItemAt(position)` to get the `ClipData.Item` then call `ClipData.Item#getUri()` to get the Uri of the Image. Sorry I have not been greater help if you have already tried this. Usually the data is in an Intent Extra Bundle or ClipData. – DeeV Jan 12 '14 at 04:11
  • ClipData clipData = intent.getClipData();//clipData will be null – qinmiao Jan 12 '14 at 04:29
  • by the way:I use emulator,don't support multiple selection and intent.getClipData() is null. so sad. – qinmiao Jan 12 '14 at 04:36

1 Answers1

21

Probably, i am little late to answer this. Might help someone who is looking for an answer to this.

    if (intent != null) {
                ClipData clipData = intent.getClipData();
                if (clipData != null) {
                    for (int i = 0; i < clipData.getItemCount(); i++) {
                        ClipData.Item item = clipData.getItemAt(i);
                        Uri uri = item.getUri();

                        //In case you need image's absolute path
                        String path= getRealPathFromURI(getActivity(), uri)
                    }
                }
            }

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null,
                null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

Note: getClipData() Call requires min API level 16

Meet Vora
  • 2,783
  • 1
  • 16
  • 33
Amar Jain
  • 1,698
  • 1
  • 14
  • 11