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. :(
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 aUri
from SAF, though, you can provide it viaEXTRA_INITIAL_URI
, and it might be honored. – Sure