简体   繁体   中英

Getting RecoverableSecurityException while deleting file on Android API 29 and 30 with requestLegacyExternalStorage = true in AndroidManifest

Below is AndroidManifest file code

<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">

Delete file code

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

In above code fileForAction.uri is specific video file uri example: "content://media/external/video/media/23"

I have tested this on Android API level 29 and 30 both. But getting android.app.RecoverableSecurityException: com.packagename.someapp has no access to content://media/external/video/media/23

As I read on this documentation https://developer.android.com/training/data-storage/shared/media#remove-item

If scoped storage is unavailable or isn't enabled, we can remove files that other apps own without handlling RecoverableSecurityException.

why I am getting RecoverableSecurityException while deleting a file when I opted for disabling scoped storage by providing android:requestLegacyExternalStorage = true in AndroidManifest

You are not deleting a file. You are deleting an entry from MediaStore that represents a piece of content. android:requestLegacyExternalStorage = "true" has nothing to do with deleting an entry from MediaStore .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM