【问题标题】:How to get the path of selected image from gallery? [duplicate]如何从图库中获取所选图像的路径? [复制]
【发布时间】:2020-05-11 02:52:57
【问题描述】:

我需要存储从图库中选择图像的路径。在吐司中,我得到了字符串 图像编码 =null。我还有一个 List 变量 imageEncodedList,它在 Toast 中也给出了“null” 选择多个图像时。我做错了什么?我想将所选图像的路径存储在android中。我还需要为 API 级别

int SELECT_PICTURES=1;
String imageEncoded;
List<String> imagesEncodedList;
select_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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"), SELECT_PICTURES);

        }
    });
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == SELECT_PICTURES && 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 = this.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();

                Toast.makeText(MainActivity.this,"one "+imageEncoded,Toast.LENGTH_LONG).show();

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

          Toast.makeText(MainActivity.this,"two"+imageEncoded,Toast.LENGTH_LONG).show();
                        imagesEncodedList.add(imageEncoded);
                        cursor.close();
                    }
                }
            }
        } else {
            Toast.makeText(this, "You haven't selected any Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }
}

【问题讨论】:

    标签: android android-intent image-gallery


    【解决方案1】:

    在 manifest.xml 中添加权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    对于 getClipData

     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);
                            Toast.makeText(this, "" + getImageFilePath(uri), Toast.LENGTH_SHORT).show();
    
                        }
                    }
    

    对于图像路径:

    public String getImageFilePath(Uri uri) {
    
            File file = new File(uri.getPath());
            String[] filePath = file.getPath().split(":");
            String image_id = filePath[filePath.length - 1];
    
            Cursor cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{image_id}, null);
            if (cursor != null) {
                cursor.moveToFirst();
                String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    
                cursor.close();
                return imagePath;
            }
            return null;
        }
    

    【讨论】:

    • getImageFilePath(Uri uri) 方法的工作原理与魅力相似
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多