How to get more control over Android SAF UI (e.g ACTION_OPEN_DOCUMENT)?
Asked Answered
B

1

6

I would like the SAF to “launch the navigator in the specified document if it's a folder” (as they call it in the docs).

This does not work so i also wonder if i can add storage shortcuts to the SAF UI. See the picture at this link: https://i.sstatic.net/adINm.jpg

But i think it is more possible to launch the SAF UI in a specific folder so i might go for that approach for the moment. I have tried this in the onCreate(...)-method:

String pathAsString = Environment.getExternalStorageDirectory() + "/myscientificapp/measurements/";
File pathAsFile = new File(pathAsString);

if (pathAsFile.exists()) { Log.d(TAG, "pathAsFile exists!"); } //This is printed in Logcat

android.net.Uri pathAsUri = Uri.fromFile(pathAsFile);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pathAsUri); //Does not make effect
startActivityForResult(intent, REQUEST_OPEN_DOCUMENT);

The thing is that it don't work.. or i do not know how to do it. :/

I have read a bit on these places. For example at Medium.com a developer says:

When selecting a location to save a file using ACTION_CREATE_DOCUMENT or when opening a document, the new EXTRA_INITIAL_URI allows you to override the default initial location (the last location the user selected) with your own custom starting location.

And on the Android docs we can read about this EXTRA_INITIAL_URI:

Callers can set a document URI through DocumentsContract#EXTRA_INITIAL_URI to indicate the initial location of documents navigator. System will do its best to launch the navigator in the specified document if it's a folder, or the folder that contains the specified document if not.

We can also read about it at DocumentsContract#EXTRA_INITIAL_URI:

EXTRA_INITIAL_URI

...
Sets the desired initial location visible to user when file chooser is shown.
...
Location should specify a document URI or a tree URI with document ID. If this URI identifies a non-directory, document navigator will attempt to use the parent of the document as the initial location.

The initial location is system specific if this extra is missing or document navigator failed to locate the desired initial location.

High abstraction languages in all its glory, but i would like to at least get some details of how to use this feature.

But here on Stackoverflow some developers tells us that it will not work.

And some threads are not really answered yet.

So according to some sources it should work.

Someone who know how to create shortcuts in the SAF UI (see the picture)? Or how to make the SAF “launch the navigator in the specified document if it's a folder” with for example ACTION_CREATE_DOCUMENT? (If i remember right, these features existed in Windows 95 for example :> )

EDIT: I removed some links to the git-project because of eventual "link-rot" as @A Jar of Clay hinted about.

I did also try to pass a tree Uri (as @CommonsWare recommended) from ACTION_OPEN_DOCUMENT_TREE onto the method intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pathAsTreeUri); . But SAF did not start in the location i passed as Uri. :(

Benempt answered 18/1, 2019 at 16:28 Comment(3)
"But here on Stackoverflow some developers tells us that it will not work" -- you can't open up a filesystem path, which is what that question was about. The documentation for EXTRA_INITIAL_URI states: "Location should specify a document URI or a tree URI with document ID". You are trying to pass in a filesystem path, which will not work. If you previously obtained a Uri from SAF, though, you can provide it via EXTRA_INITIAL_URI, and it might be honored.Sure
To avoid link-rot, it would be helpful to move the relevant parts of the GitHub code you linked into this question, and might help clarify exactly what you are looking for?Provisory
This doesn't seem to work with external storage. When specifying either of these Uris, the SAF doesn't open up in the desired location. Examples - 1) content://com.android.externalstorage.documents/document/ 2) content://com.android.externalstorage.documents/document/data.txtScipio
G
3

This answer by Alex Baker worked for me and I looks to me like it could work for you too.

DocumentFile file = DocumentFile.fromTreeUri(context, uri);
intent.putExtra(EXTRA_INITIAL_URI, file.getUri());

The resultData.getData() you get from ACTION_OPEN_DOCUMENT_TREE is the uri you start with in fromTreeUri().

Looks like in "Location should specify a document URI or a tree URI with document ID." the "with document ID" seems to be essential. I could use the resultData.getData() from ACTION_OPEN_DOCUMENT directly and it worked. Using resultData.getData with ACTION_OPEN_DOCUMENT_TREE directly didn't work, but worked after the mentioned procedure.

Anyway, worked for me, hope it works for you too.

Garfield answered 23/1, 2019 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.