Android 从库中选择多个图像

Android 从库中选择多个图像,android,android-bitmap,android-gallery,Android,Android Bitmap,Android Gallery,我正在使用此代码将意图发送到图库以加载多个图像 Intent intent = new Intent(); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChoo

我正在使用此代码将意图发送到图库以加载多个图像

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_IMAGE_MULTIPLE);
但是在ActivityResult上,我只获取映像的路径,而没有从适配器获取映像。我不知道是适配器问题还是其他问题,也不知道在哪里调试。对于活动结果,我使用此代码

try {
            // When an Image is picked
            if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                imagesEncodedList = new ArrayList<String>();
                if(data.getData()!=null){

                    Uri mImageUri=data.getData();

                    // Get the cursor
                    Cursor cursor = getContentResolver().query(mImageUri,
                            filePathColumn, null, null, null);
                    // Move to first row
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imageEncoded  = cursor.getString(columnIndex);
                    cursor.close();

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

                            ClipData.Item item = mClipData.getItemAt(i);
                            Uri uri = item.getUri();
                            mArrayUri.add(uri.toString());
                            // Get the cursor
                            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                            // Move to first row
                            cursor.moveToFirst();

                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            imageEncoded  = cursor.getString(columnIndex);
                            imagesEncodedList.add(imageEncoded);
                            cursor.close();

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

                        Toast.makeText(this, ""+mArrayUri.size(), Toast.LENGTH_SHORT).show();
                        final ArrayList<Image> images = new ArrayList<>();
                        for(int i=0;i<mArrayUri.size();i++){

                            images.add(new Image(BitmapFactory.decodeFile(mArrayUri.get(i)),
                                    new File(mArrayUri.get(i)),
                                    mArrayUri.get(i).substring(mArrayUri.lastIndexOf("/")+1)));


                        }
                        mAdapter = new ImageAdapter(this,images);

                        mAdapter.notifyDataSetChanged();
                        gridView.setAdapter(mAdapter);
                        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

                                Toast.makeText(getApplicationContext(),"Clicked short",Toast.LENGTH_LONG).show();
                            }
                        });
                        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                            @Override
                            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                                Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();

                                return false;
                            }
                        });
                    }
                    }
                }
             else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
试试看{
//拾取图像时
如果(requestCode==PICK\u IMAGE\u MULTIPLE&&resultCode==RESULT\u OK
&&空!=数据){
//从数据中获取图像
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
imagesEncodedList=新的ArrayList();
if(data.getData()!=null){
Uri mimageri=data.getData();
//获取光标
Cursor Cursor=getContentResolver().query(mimageri,
filePathColumn,null,null,null);
//移到第一排
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
imageEncoded=cursor.getString(columnIndex);
cursor.close();
}否则{
if(data.getClipData()!=null){
ClipData mClipData=data.getClipData();
ArrayList mArrayUri=新的ArrayList();
对于(int i=0;i

你到底有什么问题?您不能拾取多个图像或什么?@pskink图像不显示。图像是从图库中拾取的,但不会显示在活动中。在你的
for(int i=0;i
循环中找不到你的
Uri
问题-在
ImageView
中使用它-没有。你不必查询任何
光标,并且?你是否使用了
Uri
?如果没有,有什么不清楚的?该Uri无效。我通过另一种方法解决了它