【问题标题】:How to delete image file from Android Internal when path is stored in SQLite DB路径存储在 SQLite DB 中时如何从 Android 内部删除图像文件
【发布时间】:2018-07-23 12:38:48
【问题描述】:

我知道这是已经回答的问题。但是在使用 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”

`

【问题讨论】:

  • 删除文件即可。
  • @VladyslavMatviienko 我的应用要求也是删除路径中存储的图片
  • 图像存储在文件中。删除文件,就这样
  • Uri myUri = Uri.parse(photoPath)
  • 然后 String pathToFile = myUri.getEncodedPath();你知道其余的。看看我的回答:)

标签: android android-file


【解决方案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 
    }

【讨论】:

  • 这是删除文件的正确方法,现在通过调试检查你的路径,检查你的路径上的文件是否存在。并告诉你its not working的意思,你可以告诉我错误。
  • 我已经更新了问题中的路径,此方法不适用于我更新的路径。当我们访问存储在 SQLite DB 中的路径时。它给了我上面的路径,我无法提取实际路径。谢谢:)\
  • 您正在从媒体存储中获取文件,需要不同的方法来删除。
  • 确保您通过cursor.getColumnIndex(MediaStore.MediaColumns.DATA)获取路径,这将返回文件的绝对路径。
  • 顺便说一下 file.delete 方法对你没有帮助,因为它从目录而不是媒体存储中删除文件。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多