In Android Q, apps that aren't the default file manager or gallery can only modify and/or delete image files which they own, so, which an app has created.
Granting the read/write permissions doesn’t allow to modify or to delete any file that isn’t owned by an app.
This implies that not only files created by other apps are out of reach, but also if an app gets uninstalled and then reinstalled, then this one loses the ownership over all the public files that the app previously created. So, after the re-installation it can’t modify or delete them anymore.
When wanting to modify 1 image file or to delete a bulk of multiple images files, which were previously owned by an app, but lost ownership due to a re-installation, then what is the procedure to achieve such actions (delete or modify)?
The preferable solution would be not to use the SAF file picker, in the sense of avoid requesting to the user to select and grant a location through SAF.
And if the only solution is to use the SAF file picker, then how can be triggered to directly prompt to delete a set of known specific files without requesting tree access, neither having to tell the user to browse, search, and do it himself?
what is the procedure to achieve such actions (delete or modify)?
AFAIK, your only option is to use the SAF and get rights that way.
The preferable solution would be not to use the SAF file picker, in the sense of avoid requesting to the user to select and grant a location through SAF.
That's not possible. It would be a security flaw if it were. Please understand that while you think that these are your files, from the OS' standpoint, they are just files on the device. If apps could get arbitrary modification access to arbitrary files, that would be a step backwards from the fairly insecure stuff we had previously.
how can be triggered to directly prompt to delete a set of known specific files
There is no delete-document or delete-tree UI option in SAF, though it's not a bad idea.
neither having to tell the user to browse, search, and do it himself?
That you might be able to work around. You can try this:
Step #1: Get a Uri
for one of the MediaStore
entries (e.g., use ContentUris
and one of the IDs from a query()
for your content)
Step #2: Use getDocumentUri()
to transmogrify that MediaStore
Uri
into an SAF Uri
pointing to the same content
Step #3: Put that SAF Uri
as the EXTRA_INITIAL_URI
value in an ACTION_OPEN_DOCUMENT_TREE
Intent
, and use that to try to pre-populate the tree picker to your content's directory
Step #4: Validate that the Uri
you get back from ACTION_OPEN_DOCUMENT_TREE
is the one you are expecting (it has your files, it matches the EXTRA_INITIAL_URI
, or something along those lines)
At this point, you now can delete the files using DocumentFile.fromTreeUri()
to get a DocumentFile
for the tree, and from there list the files in the tree and delete them.
Whether the Uri
that you get from Step #2 will work for EXTRA_INITIAL_URI
in Step #3 is unclear, as I haven't tried this yet (though it's on my to-do list for early next week...).