I have a question about saving files in Android Q. As far as I understand it, if I want to save a file to a public directory and want it to be accessible by other apps I need to use the Storage Access Framework and the Android's file picker. Can I set up the directory where files will be saved once for example in preference screen without having to choose the directory every time a file is saving?
Thanks in advance.
Can I set up the directory where files will be saved once... without having to choose the directory every time a file is saving?
On Android 5.1+, you can use ACTION_OPEN_DOCUMENT_TREE
to allow the user to choose a base directory. From there, you can:
DocumentFile.fromTreeUri()
to get a DocumentFile
representing that treeDocumentFile
to create a child tree (e.g., directory)ContentResolver
and takePersistableUriPermission()
to have long-term access to your new child treeContentResolver
and DocumentFile
to put content into that child tree