2

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

Vitaly
  • 334
  • 4
  • 14

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Can I do this without requesting additional UI dialogs to user? – Vitaly Sep 28 '19 at 14:21
  • @RussiaDroneFlights: No, sorry. The point behind these changes is to put the user in charge of storage. – CommonsWare Sep 28 '19 at 14:22
  • 1
    This is a disaster :-(((( – Vitaly Sep 28 '19 at 14:30
  • ...it's just like on my PC: I save txt file in one folder with notepad. And can not open it for example with wordpad, word, etc... This is completely unbelievable situation. – Vitaly Sep 28 '19 at 14:44
  • 1
    @RussiaDroneFlights: "I save txt file in one folder with notepad" -- last I checked, the Windows Notepad program uses a "file save" dialog to allow the user to choose where to save the file and what to call it. That is what `ACTION_CREATE_DOCUMENT` does. "And can not open it for example with wordpad, word, etc.." -- last I checked, those programs use a "file open" dialog to allow the user to pick a document to open. That is what `ACTION_OPEN_DOCUMENT` does. – CommonsWare Sep 28 '19 at 14:49
  • It was an example if you dont understand... Try to imagine notepad with default directory for saving files – Vitaly Sep 28 '19 at 15:33
  • @RussiaDroneFlights: "Try to imagine notepad with default directory for saving files" -- last I checked, Notepad does not have a default directory for saving files. It always asks the user via a file save dialog. Admittedly, I do not use Notepad much, as I do not use Windows much, so perhaps they changed the UX in recent years. – CommonsWare Sep 28 '19 at 15:50
  • Ok, I understand :) – Vitaly Sep 28 '19 at 16:01
  • This is indeed a disgusting situation caused by the whacky Android Q! When the user can easily remove the permission to write externally, why the eff Android is causing so much unnecessary trouble for the users as well as the developers? Just to copy iOS which has a horrible user experience?! – doctorram Jan 09 '21 at 06:23