在AndroidManifest中使用requestLegacyExternalStorage = true删除Android API 29和30上的文件时获取RecoverableSecurityException

问题描述

下面是AndroidManifest文件代码

<application
    android:name=".application.SomeApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:networkSecurityConfig="@xml/network_config"
    android:hardwareAccelerated="true"
    android:resizeableActivity="true"
    android:requestLegacyExternalStorage="true"
    android:theme="@style/THEME_DEFAULT_BLUE_ACCENT">

删除文件代码

val result = CommonApp.getContext().contentResolver.delete(fileForAction.uri,"${MediaStore.MediaColumns._ID} = ?",arrayOf(fileForAction.id.toString()))
                if(result > 0){ successDeleted++ } else { failedDeleted++ }

在上面的代码中fileForAction.uri是具体的视频文件uri示例:“content://media/external/video/media/23”

我已经在 Android API 级别 29 和 30 上对此进行了测试。但是得到 android.app.RecoverableSecurityException: com.packagename.someapp has no access to content://media/external/video/media/23

当我阅读本文档时 https://developer.android.com/training/data-storage/shared/media#remove-item

如果范围存储不可用或未启用,我们可以删除其他应用拥有的文件,而无需处理 RecoverableSecurityException。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)