2

From Android 11 onwards (API > 29), Environment.getexternalstoragedirectory() will no more work. Since most of the libraries are using java.io.file parameter: Is it possible to get a regular File from DocumentFile? We will have to use DocumentFile. But since it's slow, I've found over the top solution here: https://www.reddit.com/r/androiddev/comments/725ee3/documentfile_is_dead_slow_heres/

Is there any other way so that we can use the old functions of java.io.file from Android 11 onwards? How can we use Document Since they have provided Environment.getexternalstoragedirectory() support only Till Android Q/ Android 10 by adding android:requestLegacyExternalStorage="true" in application tag. I would like to know the best way to handle files. Possibly, the one which will be compatible with old java.io.file and will require to write less code.

To open Folder picker:

Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        startActivityForResult(Intent.createChooser(i, "Choose Directory"), OPEN_DIRECTORY_REQUEST_CODE);

OnActivityResult:

getContentResolver().takePersistableUriPermission(
                       data.getData(),
                       Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
               );
DocumentFile documentFile = DocumentFile.fromTreeUri(this, data.getdata);

FileNotFound if I try get file using Environment.getexternalstoragedirectory()

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
cooldev
  • 419
  • 1
  • 4
  • 13

1 Answers1

4

Since most of the libraries are using java.io.file parameter

Hopefully, most of those libraries will be rewritten to support other data sources.

Is there any other way so that we can use the old functions of java.io.file from Android 11 onwards?

Limit yourself to the portions of the filesystem for which you have read/write access via methods on Context:

  • getFilesDir()
  • getCacheDir()
  • getExternalFilesDir() and getExternalFilesDirs()
  • getExternalCacheDir() and getExternalCacheDirs()
  • getExternalMediaDir() and getExternalMediaDirs()

For those directory trees, you can use File.

Otherwise, in general, you treat the rest of the device no different than you would treat the Web: get an InputStream and "download" the content.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can you share InputStream way Sample code? Thank you CommonsWare, I already went through your blog https://commonsware.com/blog/2019/03/28/death-external-storage-why.html & found it pretty helpful and awesome! Keep up with good work. – cooldev Sep 05 '19 at 12:48
  • @cooldev: "Can you share InputStream way Sample code?" -- `getContentResolver().openInputStream(yourUri)` gives you an `InputStream` on the content identified by the `Uri`. Call `getContentResolver()` on a `Context`. – CommonsWare Sep 05 '19 at 13:10
  • 1
    @CommonsWare for example, whatsapp is currently storing the data in external storage right? In that case how will they handle it? If I want to create my own folder in external storage, how can I do that? – Vivek MVK Aug 25 '20 at 17:47
  • 1
    @VivekMVK: "whatsapp is currently storing the data in external storage right?" -- I do not use WhatsApp, so I cannot answer that. "If I want to create my own folder in external storage, how can I do that?" -- for the next ~12 months, you can use `android:requestLegacyExternalStorage="true"` on the `` element in the manifest. Beyond that, use `ACTION_OPEN_DOCUMENT_TREE` and let the user decide where the user wants the user's content stored on the user's device (or perhaps in the user's cloud services). – CommonsWare Aug 25 '20 at 18:19