I'm using the Directory Selection API, which was introduced in Android 5.0 (API level 21) to let users pick a directory to save a file to.
To select a directory I build and send an ACTION_OPEN_DOCUMENT_TREE intent like in the following code:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_OPEN_DIRECTORY);
I get the following popup:
However, if I let users choose a file using ACTION_OPEN_DOCUMENT intent:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, FILE_CHOOSER_ACTIVITY_REQUEST_CODE);
Then, I get the following popup:
I'm testing it in my Android 5.0.1 Nexus Tab 10. Why do I get different root locations using ACTION_OPEN_DOCUMENT_TREE and ACTION_OPEN_DOCUMENT? Maybe only Internal Storage Document Provider instance supports subtree selection?
I would also like allowing users pick a Google Drive directory using ACTION_OPEN_DOCUMENT_TREE.
Both ACTION_OPEN_DOCUMENT_TREE and ACTION_OPEN_DOCUMENT reference say:
When invoked, the system will display the various DocumentsProvider instances installed on the device, letting the user navigate through them.
Thank you very much in advance
The result at my end is exactely same with yours, that is, cannot select subtree on Google Drive.
Based on the Android official sample about OPEN_DOCUMENT_TREE: The system displays all DocumentsProvider instances that support subtree selection
So I think the reason is that Google Drive DocumentsProvider instance did not support handle OPEN_DOCUMENT_TREE yet.
I know this is an old issue, but the situation is still the same. The file requester opened up by ACTION_OPEN_DOCUMENT_TREE does not list providers such as Google Drive or OneDrive, However, these providers DO show up when using ACTION_OPEN_DOCUMENT.
I worked around this issue by using ACTION_OPEN_DOCUMENT and specifying that the user could select multiple files. The processing in the callback to process the selections is a bit different, but it works for me.
Here is the code to set up the intent:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Choose Photos"), RQS_OPEN_PHOTO_TREE);
And here is the onActivitryResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RQS_OPEN_PHOTO_TREE) {
ClipData clipData = data.getClipData();
Uri pUri = data.getData();
if (clipData == null && pUri == null) {
// No files were selected. Don't know if this can actually happen...
Toast.makeText(this, "No jpeg files selected", Toast.LENGTH_LONG).show();
} else if( clipData != null){ // User selected multiple files
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri();
/*
* Process the returned Uri
*/
}
}
else {
/*
* The user only selected one file
* The Uri for the file is in pUri
*/
}
}
}
}
I hope this helps other in the future....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With