I'm trying to delete a file with it's Uri, but I don't understand how to add flags on this file (I'm new on android).
My application manages both media files and others files.
I managed to delete the media files with the documentation of the Mediastore API. But for others files, it says to use the storage access framework.
So, I get file's Uri like this:
Uri contentUri = ContentUris.withAppendedId(MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL), id);
Then I want to delete it like this:
DocumentsContract.deleteDocument(getContentResolver(), uri);
However, it doesn't work cause I don't add FLAG_SUPPORTS_DELETE to the file. And I don't understand how to do that.
I read the storage access framework documentation several times as well as the various posts on stackoverflow, but I did not find anything that could help me (I am new to android).
After much research, I can provide an answer to my problem.
My application aims to list all duplicate files, and delete them through a user interface.
For the media files, I did not encounter any great difficulties, the Mediastore API is relatively easy to use.
However, for non-media files it was very complicated. Indeed, I first managed to list them via the mediastore API, but I quickly realized that it was not possible to delete them with this API.
So I then opted for saf (storage access framework), but after several rereading of the doc, I realized that I could not access the Download directory. Directory essential for the proper functioning of my application.
Then, I then tried to combine both the Mediastore API and the File API. I recovered the path of the files via mediastore, and I deleted them via the File API. However, small problem, the files were deleted from my app fine, but I could still access and view them through google's Files app.
Back to the wall, I didn't know how to get out of it. Being new to android I have no experience (this is the first application I made). But I still found a "trick" to delete a non media file:
getContentResolver().delete(path, null, null);
This line of code allows me to delete a file (both from my application, and from google's Files application). However, I have no idea whether this is a bad practice or not at all. I also saw that I can use this to delete a file:
getApplicationContext().deleteFile(filename);
But I still don't know if this is a bad practice.
Here is my journey to "solve" this problem: how to delete a non media file without using saf (storage access framework) (because unable to access the Download directory)?
If you have any remarks, advice or other, I am interested. I'm also curious if you have any other techniques to solve my problem, and if my solution is good or bad.
Be indulgent, i'm new on android, and I code on android 11 (android R) only.
Thank you very much for your reading.