qinmiao
qinmiao

Reputation: 5789

select-multiple-images-from-android,how to get Uris?

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;

        }

    }

Upvotes: 8

Views: 8115

Answers (1)

Amar Jain
Amar Jain

Reputation: 1738

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

Upvotes: 21

Related Questions