Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.imgur.com/yGtcGMM.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. :(

like image 788
Philip G Avatar asked Oct 27 '22 21:10

Philip G


1 Answers

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.

like image 185
SchmadenWTGGTTTGG Avatar answered Nov 03 '22 02:11

SchmadenWTGGTTTGG