4

Android API 21 introduced new framework called SAF (Storage Access Framework) for receiving access to storage. After API 30 this is single possible solution for getting access to external storage by your app.

I am faced with 2 bugs (?) in that framework when using ACTION_OPEN_DOCUMENT_TREE intent.

  1. After user grants access to required folder to the app, provided Uri becomes initial location for all next calls of this intent, DocumentsContract.EXTRA_INITIAL_URI is totally ignored.
  2. Button "USE THIS FOLDER" is hidden for the initial folder in all next calls of this intent. For selecting the same folder user needs to go up to the parent directory, after that go back and only in this case button will appear.

Does anybody have the same problems?

My code:

 public void openDirectory() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, "content://com.android.externalstorage.documents/tree/2C13-1335%3AMusic");
    startActivityForResult(intent, FOLDER_ACCESS_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FOLDER_ACCESS_REQUEST_CODE && resultCode == RESULT_OK) {           
        if (data != null) {
            Uri uri = data.getData();  // there i receive uri of selected folder, it is "content://com.android.externalstorage.documents/tree/2C13-1335%3AMusic"
            getContentResolver().takePersistableUriPermission(uri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION |
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        }
}

With this code both bugs are present. If instead of

 intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, "content://com.android.externalstorage.documents/tree/2C13-1335%3AMusic");

I write

intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, "content://com.android.externalstorage.documents/document/primary:");

Expolorer is opened at internal storage and button is visible, i.e. both bugs are absent. And if i write

intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, "content://com.android.externalstorage.documents/document/secondary:");

Explorer is opened at cached (i.e. last selected) folder, but button is present, so i have only 1st bug

borune
  • 548
  • 5
  • 21
  • This may help: https://stackoverflow.com/a/63760593/3466808 – Sam Chen Mar 29 '21 at 15:14
  • This helps to solve 2nd bug, ty – borune Mar 29 '21 at 16:38
  • I'm not quite understand your first question, what is the "provided Uri?" With the answer I gave you you don't any `Uri` to access the folder, you just need the path in `String` type. – Sam Chen Mar 29 '21 at 19:00
  • `DocumentsContract.EXTRA_INITIAL_URI is totally ignored.` Please tell which uri you put with it. – blackapps Mar 29 '21 at 19:18
  • @SamChen provided uri is Uri, which is returned in OnActivityResult, i.e. Uri of the folder selected by user – borune Mar 30 '21 at 18:46
  • @blackapps I put there Uri, returned in OnActivityResult. For example, i select Music folder, receive its Uri in OnActivityResult, then copy it. After that selection in all futher ACTION_OPEN_DOCUMENT_TREE calls that Uri becomes the initial. Then i select another folder, for example, Documents. And all next calls of the intent open explorer window in Documents folder (seems there is some caching of last selected folder). And if I put copied Uri of Music folder in DocumentsContract.EXTRA_INITIAL_URI extra, it is ignored and explorer opens in Documents folder – borune Mar 30 '21 at 18:53
  • You could post complete code. – blackapps Mar 30 '21 at 20:08
  • @blackapps i've added the code, it is standard code for similar cases and i don't see how it could help you – borune Mar 30 '21 at 21:54
  • What is the Android version of used devices? Do you take persistable uri permissions? Not that i know if that is needed.. – blackapps Mar 31 '21 at 05:24
  • @blackapps I run this code on emulator with 30 API. Yes, after receiving the permission I call takePersistableUriPermission (i've updated the code) – borune Mar 31 '21 at 13:30
  • `EXTRA_INITIAL_URI` is not ment for `ACTION_OPEN_DOCUMENT_TREE` so if it does not work then be not surprised. If you change your subject to: How to use EXTRA_INITIAL_URI with ACTION_OPEN_DOCUMENT_TREE i will post an answer. – blackapps Mar 31 '21 at 15:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230607/discussion-between-borune-and-blackapps). – borune Mar 31 '21 at 18:36
  • Why? I will not chat. It is no bug to begin with. It is an unimplemented feature. – blackapps Mar 31 '21 at 18:39
  • At first you are liying. According the docs EXTRA_INITIAL_URI is ment for ACTION_OPEN_DOCUMENT_TREE. You can read it there: https://developer.android.com/reference/android/provider/DocumentsContract#EXTRA_INITIAL_URI. At second there is a bug with disappeared button at least. – borune Apr 01 '21 at 20:10
  • 2
    @borune did you find the solution? I'm running into your 1st issue as well. – The Hungry Androider Aug 03 '22 at 19:51
  • 1
    @TheHungryAndroider unfortunately i dont – borune Aug 04 '22 at 20:23

0 Answers0