当路径存储在 SQLite 数据库中时如何从 Android 内部删除图像文件

How to delete image file from Android Internal when path is stored in SQLite DB

我知道这已经回答了 question.But 我在使用 SQLite DB 时无法弄清楚。我的应用程序捕获了一些文档并将存储在 phone 内存中。我在我的应用程序中使用 SQLite DB 来存储上图的路径。如果我删除 SQLite 数据库中的图像,我如何从 phone 内存中删除图像。

String photoPath = cursor.getString(i_COL_PICTURE);

--我的路径是

`"content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F153/ORIGINAL/NONE/1743496576"

`

当您想删除存储中的某些文件时,只需执行此操作即可。

File file = new File(yourFilePathHere);
deleted = file.delete();

我认为您拥有所需的权限,因为您可以在存储中写入文件。

编辑

您正在使用 MediaStore 获取图像。所以现在当你想删除文件时,你也应该从 MediaStore 中删除文件。我有一个方法可以帮助你。

public static int deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
    String canonicalPath;
    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)) {
            int deletedRow = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
            return deletedRow;
        }
    } else return result;
    return result;
}

在你的 Activity 中调用它,比如

deleteFileFromMediaStore(getContentResolver(), fileToDelete)

注意 检查你是否通过MediaStore获取绝对路径。如果您的代码有问题,这是我获取所有图库图像的方法。

  public static ArrayList<ModelBucket> getImageBuckets(Context context) {
        ArrayList<ModelBucket> list = new ArrayList<>();
        String absolutePathOfImage;
        String absoluteFolder;
        boolean same_folder = false;
        int pos = 0;
        Uri uri;
        Cursor cursor;
        int column_index_data, column_index_folder_name;

        uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        cursor = context.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
        if (cursor == null) return 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);
            absoluteFolder = cursor.getString(column_index_folder_name);
            Log.d("Column", absolutePathOfImage);
            Log.d("Folder", absoluteFolder);

            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFolderName().equals(absoluteFolder)) {
                    same_folder = true;
                    pos = i;
                    break;
                } else {
                    same_folder = false;
                }
            }
            if (same_folder) {
                ArrayList<String> al_path = new ArrayList<>(list.get(pos).getAllFilesPath());
                al_path.add(absolutePathOfImage);
                list.get(pos).setAllFilesPath(al_path);
            } else {
                ArrayList<String> al_path = new ArrayList<>();
                al_path.add(absolutePathOfImage);
                ModelBucket modelBucket = new ModelBucket();
                modelBucket.setFolderName(absoluteFolder);
                modelBucket.setAllFilesPath(al_path);
                list.add(modelBucket);
            }
        }
        return list;
    }

这里ModelBucket.class是模型class.

public class ModelBucket {
    String folderName;
    ArrayList<String> allFilesPath;
    ArrayList<ModelFile> files;

   // make getter setter 
    }

在删除图片之前获取图片的路径并将路径传递给下面的代码

File fdelete = new File(path);

if (fdelete.exists()) {
    if (fdelete.delete()) {
        System.out.println("file Deleted :" + path);
    } else {
        System.out.println("file not Deleted :" + path);
    }
}

之后从 sqlite 数据库中删除路径

如果您的 Uri 指向您可以执行的文件:

String pathToFile = myUri.getEncodedPath(); // this gives your the real path to the file, like /emulated/0/sdcard/myImageFile.jpg

File file = new File(pathToFile);

if(file.exists()){
   file.delete();
}