Android:在SD卡上删除图片

问题描述 投票:0回答:2

Android 7.0,API 24.权限在AndroidManifest和实时授予。

以下是我尝试的方案:

  • 仅使用内容解析程序将其从MediaStore中删除,但在重新启动设备时会返回。
  • 当删除内部图像“/ storage / emulated / 0 / DCIM / Camera / ...”时它可以工作,但是当试图删除外部图像(或应该是什么外部图像)时“/ storage / 4ED7-7F17 / DCIM / Camera / ...“它在file.canWrite()失败。
  • 使用Environment.getExternalStorageDirectory().getAbsolutePath()返回"/storage/emulated/0" (+ "/DCIM/Camera/..."),它在file.exists()失败。
  • 硬编码"/SD card/DCIM/Camera/..."(应该是正确的文件路径)在file.exists()失败。

奇怪的是,在Android设备文件资源管理器中,应该在SD卡文件夹中的文件位于“/ storage / 4ED7-7F17 /”文件夹中,文件的权限列表为-rwxrwx-x。 “/ storage / emulated /”中的权限是“Permission denied”。但要在Android应用程序MyFiles或Windows文件资源管理器中查找文件,文件位于“/ SD卡/ DCIM / Camera / ...”中。

完全混淆任何帮助将不胜感激。

File file = new File(filename);
    if (file.exists()) {
        if (file.canWrite()) {
            if (file.delete()) {
                // Set up the projection (we only need the ID)
                String[] projection = {MediaStore.Files.FileColumns._ID};
                // Match on the file path
                String selection = MediaStore.Files.FileColumns.DATA + " = ?";
                String[] selectionArgs = new String[]{filename};
                Uri queryUri;
                if (isVideo) {
                    // Query for the ID of the media matching the file path
                    queryUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else {
                    // Query for the ID of the media matching the file path
                    queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                }
                ContentResolver contentResolver = context.getContentResolver();
                Cursor c = contentResolver.query( queryUri, projection, selection, selectionArgs, null);
                if (c.moveToFirst()) {
                    // We found the ID. Deleting the item via the content provider will also remove the file
                    long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID));
                    Uri deleteUri = ContentUris.withAppendedId(
                                        MediaStore.Files.getContentUri("external"), id);
                    contentResolver.delete(deleteUri, null, null);
                } else {
                    // File not found in media store DB
                    Toast.makeText(context, "File not found: " + filename,
                            Toast.LENGTH_LONG).show();
                }
                c.close();
                Toast.makeText(context, "File deleted: " + filename,
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "File not deleted: " + filename,
                        Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(context, "File cannot be wrote to: " + filename,
                    Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(context, "File does not exist: " + filename,
                Toast.LENGTH_LONG).show();
    }
}
android delete-file android-gallery android-external-storage
2个回答
0
投票

)“storage / 4ED7-7F17 / DCIM / Camera / ...”在file.canWrite()失败。

当然是。 Micro SD卡仅适用于现代Android系统。

现在的应用程序只能在SD卡上的一个应用程序特定目录中写入。

如果您想要对整个卡进行写访问,则需要使用存储访问框架。


0
投票

我说得对。 N上缺少的是MediaStore.getDocumentUri(Context context, Uri mediaUri),它将Oreo +上DCIM目录的虚构文件路径转换为可用于删除文件的Storage Access Framework URI。 N似乎没有提供我能找到的等价物。除非转换,否则内容:媒体文件的Uris不能与Storage Access Framework一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.