Pick multiple images from gallery

烈酒焚心 提交于 2019-12-08 06:22:30

问题


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

回答1:


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



回答2:


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



回答3:


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


        }


来源:https://stackoverflow.com/questions/27372106/pick-multiple-images-from-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!