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

时间:2018-07-23 12:38:49

标签: android android-file

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

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”

`

3 个答案:

答案 0 :(得分:1)

要删除存储中的某些文件时,只需执行此操作。

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是模型类。

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

   // make getter setter 
    }

答案 1 :(得分:0)

在删除图像之前,获取图像的路径并将该路径传递给以下代码

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 db中删除路径

答案 2 :(得分:0)

如果您的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();
}