Reputation: 863
So I have an app which is a music player.
Many times a user plays a song and wants to delete it. Many times he finds that there duplicates of files and hence wants to delete it. Many users have more than thousands of songs and it would not be an appropriate solution if the user has to to pin point the location of the file he wants to delete.
I came across this How to use the new SD card access API presented for Android 5.0 (Lollipop)?
and it tells how to create the files given the whole uri.
But The appropriate solution would be if he could just choose the root directory(SD CARD ) instead of the whole path and grant the required permission and the app could manage the rest. This is exactly I want to achieve.
Given that i have only the file path of the file to be deleted and the uri of the root directory from the sd card access framework, what is the best way of deleting the file? is that even possible ? or each time a user has to delete a file, he will have to pin point the location of that file as well?
Upvotes: 1
Views: 714
Reputation: 1669
The link you mentioned (How to use the new SD-Card access API presented for Lollipop) gives instructions on how to prompt user to choose the root directory using ACTION_OPEN_DOCUMENT_TREE
.
After the user chooses the root node then your code will have a DocumentFile that represents the root node. You say you already have the file path. If so then use the segments of the file path and follow the path down the hierarchy using DocumentFile.html#listFiles().
You'll finally have the DocumentFile that represent the file you want to delete, then call DocumentFile.html#delete()
Upvotes: 2