I'm trying to delete audio files from the device's external storage (for example in the /storage/emulated/0/Music folder).
After analyzing the MediaStore sample, I ended up with the following solution for API 28 and earlier:
fun deleteTracks(trackIds: LongArray): Int {
val whereClause = buildWildcardInClause(trackIds.size) // _id IN (?, ?, ?, ...)
return resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, whereClause, Array(trackIds.size) { trackIds[it].toString() })
}
I noticed that the above code only deletes tracks from the MediaStore database but not from the device (files are re-added to the MediaStore after rebooting). So I modified that code to query the Media.DATA column, then used that information to delete the associated files. This works as intended.
But now that Android Q introduced Scoped Storage, Media.DATA (and Albums.ALBUM_ART) are now deprecated because the app may not be able to access those files. ContentResolver.openFileDescriptor can only be used to read files, not delete them.
Then what is the recommended way to delete tracks as of Android Q ?
The sample does not show how to delete multiple files from the MediaStore, and MediaStore.Audio.Media seems to work differently than MediaStore.Images.Media.