Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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;

        }

    }
like image 423
qinmiao Avatar asked Jan 12 '14 03:01

qinmiao


People also ask

How do you select multiple pictures on Android?

To grab several at once, you can enter selection mode by long-pressing on one photo, and then tapping on other pictures or on a date. Doing the latter will automatically select all the images taken on a specific day.

How do you select multiple images?

For selecting multiple images in the gallery with Android Jetpack Compose. And then use launcherMultipleImages. launch("image/*") to start the images selection.

How do I select multiple files in Android programmatically?

To select multiple files press on as many files as you want to select and check marks will appear over all of the selected files. OR you press the More options menu icon in the upper right corner of the screen and press Select.


1 Answers

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

like image 197
Amar Jain Avatar answered Oct 20 '22 10:10

Amar Jain