Pick multiple images from gallery

VVB picture VVB · Dec 9, 2014 · Viewed 11.9k times · Source

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();

Answer

Herry picture Herry · Dec 9, 2014

@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);
    }