Reputation: 438
I want to get access to public directory files, for storing not: image, audio and video! I need to store: documents (for example: doc, pdf, bak, zip, etc...). I need these documents should stay on user device even after my app will be uninstalled. And I need user can access these document files from other apps.
In early versions of android, for documents folder, I can simply write down next line:
File myDirectory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "My documents");
if (!myDirectory.exists()) {
if(myDirectory.mkdirs()){
// directory exists, can place any files here
}
}
How can I do this in android Q? Or no way, and I just need to tell my users that my app doesnt support Android 10 at all?
p.s. I see this: getExternalStoragePublicDirectory deprecated in Android Q - No answer for my question
Upvotes: 2
Views: 1788
Reputation: 1006604
How can I do this in android Q?
Use ACTION_CREATE_DOCUMENT
to allow the user to choose a location, and then store your content at the Uri
that you get.
Or, use ACTION_OPEN_DOCUMENT_TREE
to allow the user to choose a tree (e.g., directory). You can use the Uri
that you get back with DocumentFile.fromTreeUri()
to be able to create child documents. In each case, you wind up with a Uri
that you can use to store the content.
Given a Uri
, you can use ContentResolver
and openOutputStream()
to write the content to the location identified by the Uri
.
This sample Java app (and its Kotlin counterpart) demonstrate how to work with files and content Uri
values. I use ACTION_OPEN_DOCUMENT
to allow the user to pick an existing text file or ACTION_CREATE_DOCUMENT
to allow the user to create a new text file.
Upvotes: 1