The documentation says \"Callers can set a document URI through EXTRA_INITIAL_URI
to indicate the initial location of documents navigator.\"
But it won
From DocumentsContract.EXTRA_INITIAL_URI:
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.
As far as I'm aware
Uri.fromFile
, it needs to originate from a DocumentsProviderAssuming uri
was retrieved from Intent.ACTION_OPEN_DOCUMENT_TREE:
DocumentFile file = DocumentFile.fromTreeUri(context, uri);
intent.putExtra(EXTRA_INITIAL_URI, file.getUri());
I think you need a content URI like "content:/some/uri"
for this to work.
The following is based on the answer in question MediaStore - Uri to query all types of files (media and non-media):
String getFileContentUri(ContentResolver contentResolver, File file){
String filePath = file.getAbsolutePath();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = {MediaStore.Files.FileColumns.DATA};
String selection = MediaStore.Files.FileColumns.DATA+"=? ";
String[] selectionArgs = new String[] { filePath };
String sortOrder = null;
Cursor fileCursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
if(fileCursor != null && fileCursor.moveToFirst()){
int columnIndex = fileCursor.getColumnIndex(projection[0]);
filePath = fileCursor.getString(columnIndex);
fileCursor.close();
return filePath;
}
return null;
}
Also you probably need to request the permission android.permission.READ_EXTERNAL_STORAGE
.