I can successfully obtain a basepath Uri from OPEN_DOCUMENT_TREE from the Storage Access Framework.
How to use the new SD card access API presented for Android 5.0 (Lollipop)?
private static final int READ_REQUEST_CODE = 42;
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, READ_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
// response to some other intent, and the code below shouldn't run at all.
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
}
}
}
But there is a bug/feature in android 5.0 that breaks recursion as referenced in this post:
Bug when listing files with Android Storage Access framework on Lollipop
Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
Uri f1 = pickedDir.findFile("MyFolder").getUri();
Log.d(TAG, "f1 = " + f1.toString());
Using File.listFiles() returns a Null array.
I already know the full path to the target folders/files. I would like to construct a valid DocumentFile Uri which has the authority of the root Uri returned in onActivityResult.
I would like to append to the root Uri path or build a new Uri which has the same permissions as the root Uri to access the target folders/files.