Search code examples
androidandroid-11storage-access-framework

How to open Documents folder using Storage Access Framework on Android 11 and higher?


This question only concerns Android 11 and above.

Reading about Android Storage Framework at https://developer.android.com/guide/topics/providers/document-provider, I am able to make my app create a file in the public Downloads directory of my app.

Doing the following opens the Downloads location for me and I am able to save a file.

private static final int WRITE_REQUEST_CODE = 101;

Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TITLE, "someFile.txt");
startActivityForResult(i, WRITE_REQUEST_CODE);

Above works great! But is the technique too different when I want my app to open the Documents folder instead of Downloads? How can I make my app open Documents folder on an Android 11 phone when the system dialog opens and be able to save the file there?

Please note that the intention is to create a document in Documents folder of the Android device.


Solution

  • Doing the following opens the Downloads location for me

    Not necessarily. There is no guaranteed "point of entry", and you should not assume any particular location.

    How can I make my app open Documents folder on an Android 11 phone when the system dialog opens

    Unless you had previously gotten a Uri for Documents/ from ACTION_OPEN_DOCUMENT_TREE/ActivityResultContracts.OpenDocumentTree, you do not have a way to do that.

    Note, though, that users can go to Documents/ or any other tree of their choosing through the UI that is opened by your code. Unfortunately, that UI is not very intuitive for newcomers.