如果路径格式为`/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg`

问题描述

所以我试图从Android外部存储中删除文件,问题是,我试图以多种不同的方式多次获取路径。使用MediaStore Api,我确实很接近,但无法弄清楚该怎么做。 我无法获得EXTERNAL_URI,因为投影需要字符串,而MediaStore上的EXTERNAL_URI返回的是URI,而不是字符串。

这就是我加载图像的方式,galleryImage类仅包含该图像,文件路径为/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg,还有一些布尔值来处理选择。我尝试添加外部URI,但未成功。

public class ImageLoader {
    public ArrayList<galleryImage> getAllShownImagesPath(Context context) {
        Uri uri;
        Cursor cursor;
        int column_index_data,column_index_folder_name,column_index_content_uri;
        ArrayList<galleryImage> listofAllImages = new ArrayList<galleryImage>();
        String absolutePathOfImage = null;
        uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = { MediaStore.MediaColumns.DATA,MediaStore.Images.Media.BUCKET_disPLAY_NAME,};

        cursor = context.getContentResolver().query(uri,projection,null,null);

        column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        column_index_folder_name = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_disPLAY_NAME);

        while (cursor.movetoNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);
         //   String externalURI = cursor.getString();
            String externalURI = ""; //empty,beacuse I Couldnt get it to work...

            listofAllImages.add(new galleryImage(false,false,absolutePathOfImage,externalURI));
        }


        cursor.close();

        return listofAllImages;
    }
}

在我的片段中,我试图删除图像

 if(item.getItemId() == R.id.delete_selected){
                    //Uri root = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    String root = Environment.getExternalStorageDirectory().toString();
                    for(galleryImage i : mviewmodel.selectedImages){
                        File file = new File(root + i.getmImageUrl());
                        if (file.exists()) {
                            if (file.delete()) {
                                System.out.println("file Deleted :" + file.getPath());
                            } else {
                                System.out.println("file not Deleted :" + file.getPath());
                            }
                        }
                    }
                }

我尝试了4,5种不同的组合,但是似乎都不起作用,我试图获取正确的图像路径,我发现也许可以使用EXTERNAL_URI,但是我不知道如何将其放入我的galleryImage中。任何帮助表示赞赏!

解决方法

    public void deleteSelectedImages(){
        for(GalleryImage img : selectedImages){
            File file = new File(img.getmImageUrl());
            if(!file.exists()) {
                return;
            }

            String canonicalPath;
            ContentResolver contentResolver = getApplication().getContentResolver();
            try {
                canonicalPath = file.getCanonicalPath();
            } catch (IOException e) {
                canonicalPath = file.getAbsolutePath();
            }
            final Uri uri = MediaStore.Files.getContentUri("external");
            final int result = contentResolver.delete(uri,MediaStore.Files.FileColumns.DATA + "=?",new String[]{canonicalPath});
            if (result == 0) {
                final String absolutePath = file.getAbsolutePath();
                if (!absolutePath.equals(canonicalPath)) {
                    contentResolver.delete(uri,new String[]{absolutePath});
                }
            }

            images.remove(img);
        }

        imagesLiveData.setValue(images);
        selectedImages.clear();
        Toast.makeText(getApplication().getApplicationContext(),"Succesfully deleted image(s)",Toast.LENGTH_SHORT).show();
    }

希望它将对某人有所帮助