I have been reading the android documentation and I can't find a solution to delete multiple files without multiple dialogs in Android 10 (API Level 29)
I found the following in the Android documentation:
- API Level < 29: Delete files without dialog, permission granted on app startup
- API Level > 29: Delete files by showing all files inside a dialog which the user has to confirm
- API level = 29: Delete files by showing a permission dialog for each file
This is very frustrating not only for me but for every user who has API level 29, because there are some use cases where up to 100+ files must be deleted.
Entry Point when wanting to delete media
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
deleteMediaR(activity, uris)
}
Build.VERSION.SDK_INT == Build.VERSION_CODES.Q -> {
deleteMediaQ(activity, uris)
}
else -> {
deleteMediaDefault(activity, uris)
}
Above API Level 29
@RequiresApi(Build.VERSION_CODES.R)
private fun deleteMediaR(activity: Activity, uris: ArrayList<String>) {
val contentResolver = activity.contentResolver
val collection: ArrayList<Uri> = ArrayList()
collection.addAll(uris.map { uri -> Uri.parse(uri) })
val pendingIntent = MediaStore.createDeleteRequest(contentResolver, collection)
activity.startIntentSenderForResult(
pendingIntent.intentSender, 42, null, 0, 0, 0, null)
}
API Level 29
@RequiresApi(Build.VERSION_CODES.Q)
private fun deleteMediaQ(activity: Activity, uris: ArrayList<String>) {
try {
deleteMediaDefault(activity, uris)
} catch (exception: Exception) {
if (exception is RecoverableSecurityException) {
val pendingIntent: PendingIntent = exception.userAction.actionIntent
activity.startIntentSenderForResult(pendingIntent.intentSender,
42, null, 0, 0, 0, null)
}
}
}
Below API Level 29
contentResolver.delete(uri, where, media))
This is a very frustrating problem only concerning Android 10. I'm assuming Google forgot to implement this feature into their API. However, I'm hoping there is a proper solution as this breaks the app when using Android 10.