Search code examples
androidstorage-access-frameworkandroid-storage

How to open SAF (Storage Access Framework) to select storage?


How to open SAF (Storage Access Framework) to select storage as in the picture? enter image description here

I tried:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra(DocumentsContract.EXTRA_PROMPT, "Select path to save");
startActivityForResult(intent, PICK_FOLDER);

but this opens only Recent and user have to click to ... and Show Internal Storage and after that select for example SDCard or Internal Storage. That's not user friendly.

Is there any way to open view as in the picture or not? There is extra data to Intent DocumentsContract.EXTRA_INITIAL_URI, but what value can I add it to show open view as in the picture?

I can direct open primary storage via:

StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { //From Android 10+
    startActivityForResult(sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent(), PICK_FOLDER);
}

but I would like to open view as in the picture.


Solution

  • How to open SAF (Storage Access Framework) to select storage as in the picture?

    My guess is that "Select path to save" will mean that UI came from ACTION_CREATE_DOCUMENT. If you are expecting the drawer to be opened automatically, though, there is no Intent action that does that.

    but this opens only Recent and user have to click to ... and Show Internal Storage and after that select for example SDCard or Internal Storage. That's not user friendly.

    There is no documented and supported means of automatically offering "internal storage" as an option. That being said, there are hacks that you can try, bearing in mind that there is no guarantee that they will work across devices, OS versions, etc. And none necessarily open that drawer.

    There is extra data to Intent DocumentsContract.EXTRA_INITIAL_URI, but what value can I add it to show open view as in the picture?

    Um, none? EXTRA_INITIAL_URI would pre-select some document (where the Uri is one you got via the SAF previously), but it would not necessarily change the storage options and it would not necessarily pre-open that drawer.