gpt4 book ai didi

android - 我们可以使用 MediaStore API 删除图像文件吗?如果是,那么如何

转载 作者:行者123 更新时间:2023-12-04 23:49:09 26 4
gpt4 key购买 nike

我需要在我的应用程序中使用后台服务一段时间后删除屏幕截图图像文件,并且使用上述方法可以正常工作

private void deleteTheFile(String path) {
File fdelete = new File(path);
if (fdelete.exists()) {
if (fdelete.delete()) {
getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path))));
Log.i(TAG, "deleteTheFile: file deleted");
} else {
Log.i(TAG, "deleteTheFile: file not dellleeettteeeddd");
}
}
但众所周知,android R (11) 带来的变化
所以我尝试用

MANAGE_EXTERNAL_STORAGE permission


但谷歌拒绝了我的更新说

Issue: Need to use Media Store API

You have requested access to All Files Access permission but itappears that your app's core feature requires access to only MediaFiles. With the MediaStore API, apps can contribute and access mediathat's available on an external storage volume without the need forthe access all files permission.

Please update your app so that the feature uses Media Store APIs andremove All Files Access (MANAGE_EXTERNAL_STORAGE) permission.


但我以前从未使用过媒体存储 API,我不知道它可以用它删除图像文件,因为删除文件属于可写部分

最佳答案

使用 createDeleteRequest

private fun deleteImages(uris: List<Uri>) {
val pendingIntent = MediaStore.createDeleteRequest(contentResolver, uris.filter {
checkUriPermission(it, Binder.getCallingPid(), Binder.getCallingUid(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED
})
startIntentSenderForResult(pendingIntent.intentSender, REQ_CODE, null, 0, 0, 0)
}
使用内容解析器
// Remove a specific media item.
val resolver = applicationContext.contentResolver

// URI of the image to remove.
val imageUri = "..."

// WHERE clause.
val selection = "..."
val selectionArgs = "..."

// Perform the actual removal.
val numImagesRemoved = resolver.delete(
imageUri,
selection,
selectionArgs)
https://github.com/android/storage-samples/tree/main/MediaStore
这是一个 android 官方示例,您可以了解并尝试使用 MediaStoreAPI 实现它

关于android - 我们可以使用 MediaStore API 删除图像文件吗?如果是,那么如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67798210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com