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()