如何仅通过内容 URI 删除文件?

huangapple 未分类评论43阅读模式
英文:

How can i delete a file with only the content uri?

问题

在 API 29 之前,我使用从 MediaStore.Audio.Media.DATA 列中获取的路径来删除文件,但该列现已弃用。

现在我这样做

song.setData(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))));

但这会返回一个类似于这样的 内容 URIcontent://media/external/audio/media/92079

在之前,我只需对内部存储上的文件执行 file.delete 操作,对外部存储上的文件执行 documentFile.delete() 操作。

但现在我只有 内容 URI,而没有媒体文件的路径,所以不能再这样操作了。

那么,如何在不依赖于 MediaStore.Audio.Media.DATA 获得媒体文件路径的情况下删除文件呢?

在 API 29 之前使用 DATA 列的完整代码

private void deleteFile(Uri uri, File file){
    if (file.delete()){
        Log.i(TAG, "内部删除!");
        return;
    }
    if (getActivity() != null && file.exists()) {
        DocumentFile pickedDir = DocumentFile.fromTreeUri(getActivity(), uri);
        String filename = file.getName();
        try {
            getActivity().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } catch (SecurityException e) {
            Log.e(TAG, "删除文件路径时捕获到 SecurityException!", e);
        }
        Log.d(TAG, "文件名:" + filename);
        if (pickedDir != null) {
            DocumentFile documentFile = pickedDir.findFile(filename);
            if (documentFile != null && documentFile.exists()) {
                if (documentFile.delete()) {
                    Log.i(TAG, "删除成功");
                } else {
                    Log.i(TAG, "删除失败");
                }
            }
        }
    }
}
英文:

Before API 29 i deleted files with the path i got from MediaStore.Audio.Media.DATA column which is now deprecated.

Now i do it like this

song.setData(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))));

But this returns a content uri which looks like this: content://media/external/audio/media/92079

Before i just did file.delete for files on internal storage and documentFile.delete() for files on external storage.

But now that i only have the content uri and not a path to the media file it doesn't work anymore.

So how can i delete files without relying on the MediaStore.Audio.Media.DATA for a path to the media file?

Full code which worked before API 29 and using the DATA column

private void deleteFile(Uri uri, File file){
        if (file.delete()){
            Log.i(TAG,"internal delete!");
            return;
        }
        if (getActivity() != null && file.exists()) {
            DocumentFile pickedDir = DocumentFile.fromTreeUri(getActivity(), uri);
            String filename = file.getName();
            try {
                getActivity().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } catch (SecurityException e) {
                Log.e(TAG, "SecurityException caught when deleting FilePath!", e);
            }
            Log.d(TAG, "fileName: " + filename);
            if (pickedDir != null) {
                DocumentFile documentFile = pickedDir.findFile(filename);
                if (documentFile != null && documentFile.exists()) {
                    if (documentFile.delete()) {
                        Log.i(TAG, "Delete successful");
                    } else {
                        Log.i(TAG, "Delete unsuccessful");
                    }
                }
            }
        }
    }

huangapple
  • 本文由 发表于 2020年4月8日 01:53:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61086316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定