从图库中选择多个图像

时间:2018-07-12 02:53:08

标签: 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.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);

但是在“活动”结果上,我仅获取图像的路径,而不从适配器获取图像。我不知道是适配器问题还是其他问题,也不知道在哪里调试。对于活动结果,我正在使用此代码

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

Loaded images

0 个答案:

没有答案