3

How to read/retrieve paths or Uri[] when I select multiple images from gallery?

I want to call this:

Uri[] originalUri = data.getData();

But in reality I'm getting this only, fetching only one Uri:

 Uri originalUri = data.getData();
halfer
  • 19,824
  • 17
  • 99
  • 186
VVB
  • 7,363
  • 7
  • 49
  • 83
  • How about saving the uri first in a ArrayList? then do a for-each loop? – JLONG Dec 09 '14 at 05:42
  • @JLONG My problem is I am unable to get that array. Please read my question carefully. It rest rives only one Uri though I selected multiple images – VVB Dec 09 '14 at 05:43
  • Sorry bout that, anyway have you looked at this link? http://www.javacodegeeks.com/2012/10/android-select-multiple-photos-from-gallery.html – JLONG Dec 09 '14 at 05:46
  • I have already gone through it, but I don't want to fetch all the gallery in my app. – VVB Dec 09 '14 at 05:49
  • @RIT For multiples image selection you have create your own activity to load all image and implement multiple selection in it. – Herry Dec 09 '14 at 05:52
  • @Herry I just said it above, I don't want to load all the images in my app. I just want to fetch selected only. – VVB Dec 09 '14 at 05:54
  • @RIT but in android they are not giving any intent that use for multiples image selection ,So let me known which code you are using to start multiples image selection. – Herry Dec 09 '14 at 06:20
  • It started from Kitkat version. But unable to get it that's why I put the question over here. – VVB Dec 09 '14 at 06:54
  • http://www.javacodegeeks.com/2012/10/android-select-multiple-photos-from-gallery.html – Shubham AgaRwal Oct 15 '15 at 12:17
  • http://stackoverflow.com/a/33228269/3649347 – Tell Me How Oct 20 '15 at 12:08

4 Answers4

12

@RIT as said by you that you want to get multiples images in andorid kitkat .

I have try below code which work for me for Xperia M2 4.4.4

For start image selection activity

private void startImageSelection(){

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGES);
    } 

But user need to select images by long press

Now to read selected images Uri use below code for onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if(requestCode==PICK_IMAGES){

            if(resultCode==RESULT_OK){
                //data.getParcelableArrayExtra(name);
                //If Single image selected then it will fetch from Gallery
                if(data.getData()!=null){

                    Uri mImageUri=data.getData();

                }else{
                    if(data.getClipData()!=null){
                        ClipData mClipData=data.getClipData();
                        ArrayList<Uri> mArrayUri=new ArrayList<Uri>();
                        for(int i=0;i<mClipData.getItemCount();i++){

                            ClipData.Item item = mClipData.getItemAt(i);
                            Uri uri = item.getUri();
                            mArrayUri.add(uri);

                        }
                        Log.v("LOG_TAG", "Selected Images"+ mArrayUri.size());
                    }

                }

            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }
Herry
  • 7,037
  • 7
  • 50
  • 80
0

for selecting multiple images from gallery you can use the following code

String[] projection = {
        MediaStore.Images.Thumbnails._ID,
        MediaStore.Images.Thumbnails.IMAGE_ID
};

mCursor = getContentResolver().query(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        projection,
        null,
        null,
        MediaStore.Images.Thumbnails.IMAGE_ID + " DESC"
);

columnIndex = mCursor.getColumnIndexOrThrow(projection[0]);
Miller
  • 744
  • 3
  • 15
  • 39
0
Cursor cursor = this.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,
                null, null);

        cursor.moveToFirst();

        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            int cols = cursor.getColumnCount();
            String columns = null;
            for (int j = 0; j < cols; j++) {
                columns += cursor.getColumnName(j) + " | ";
            }

            imagePathUriList.add(cursor.getString(1));


        }
TechHelper
  • 822
  • 10
  • 24
0

here's my take of it in Kotlin. This call is from a fragment, and should work in an activity with little to no tweaking.

//const lives in companion or above class definition
private const val PHOTO_PICKER_REQUEST_CODE = 36

private fun launchPhotoPicker() {
    Intent(Intent.ACTION_PICK).apply {
        putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        type = "image/*"
    }.let {
        startActivityForResult(it, PHOTO_PICKER_REQUEST_CODE)
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == PHOTO_PICKER_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            val selectedPhotoUris = mutableListOf<Uri>()

            data?.data?.let {
                selectedPhotoUris.add(it)
            }

            data?.clipData?.let { clipData ->
                for (i in 0 until clipData.itemCount) {
                    clipData.getItemAt(i).uri.let { uri ->
                        selectedPhotoUris.add(uri)
                    }
                }
            }

            //data.data is usually the first item in clipdata
            //I prefer to not make assumptions and just squash dupes
            //I don't trust like that
            val uris = selectedPhotoUris.distinctBy { it.toString() }

            handlePhotoUris(uris)
        }
    }

    super.onActivityResult(requestCode, resultCode, data)
}

private fun handlePhotoUris(uris: List<Uri>) {
    Log.d("photoPicking", "handle uris size: ${it.size}")
    //do stuff here
}
Alex Mousavi
  • 283
  • 2
  • 4
  • 14