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
.
(that were copied to the device from USB) from the external storage.
It is unclear where those files whould reside after having been copied. You meen TO external storage? Where exactly? And from USB is unclear too until you explain how exactly. – Vivie/storage/emulated/0/Music
folder. I want to delete them programmatically from my app. I've edited the question to make it clearer. – Pt/storage/emulated/0/Music
then you can certainly delete them your self. Why messing around with the MediaStore? – Vivie/storage/emulated/0/Music
. Well if so then you 'saw' that folder and you can delete those files again. Please elaborate. – Vivie