Using ContentResolver
to delete media files is wrong and provides many problems for the user.
You can not delete a file on the sd-card
simply by deleting its information from the ContentResolver
on Android versions greater than Jelly Bean(4.3)
.
It works only on Android versions prior to KitKat(4.4)
.
That's why the Android team provided DocumentProvider.
Why contentResolver.delete(...)
is wrong?
1. Fills up the sd-card
When you try to delete a media file on the sd-card
by the ContentResolver
on Android versions greater than 4.3, the actual media file will remain untouched because the contentResolver.delete(...)
approach only removes the information (name, date, path ...) of the media and you will end up having unregistered media files on your sd-card
which ContentResolver
has no idea about their existence anymore and that's why you couldn't see them in your gallery and you think they've been deleted with this approach while they're still there and fill up the sd-card
each time the user tries to delete a media file on the sd-card
.
2. Media files (Images, videos, gifs ...) will come back to the gallery
There are many apps out there especially gallery and file manager ones that will find these unregistered media files and will add them to the ContentResolver
again as of their normal behavior while the user assumes his/her unwanted media files are gone.
Sure no user wants his/her assuming deleted images or videos show up in the middle of a demonstration.
So, what's the correct approach to remove media files on the sd-card
?
Well, this has already been answered here with the use of DocumentProvider
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…