问题
This is a Xamarin project but I think the code mostly just wraps the android sdk. When I pick a file I use/get this:
var intent = new Intent(Intent.ActionGetContent);
intent.AddCategory (Intent.CategoryOpenable);
StartActivityForResult (Intent.CreateChooser (intent, "Select file"), 0);
Notice how it is capable of opening from remote locations. But when try to pick a directory I use/get this:
// https://stackoverflow.com/questions/27889377/action-open-document-tree-only-returns-empty-recent-folder
var intent = new Intent(Intent.ActionOpenDocumentTree);
intent.PutExtra("android.content.extra.SHOW_ADVANCED", true);
intent.PutExtra("android.content.extra.FANCY", true);
intent.PutExtra("android.content.extra.SHOW_FILESIZE", true);
StartActivityForResult (Intent.CreateChooser (intent, "Select Save Location"), 0);
Am I missing something or is there no way to do this? For example, when choosing the save location I would like for the user to be able to choose Prime Photos if they have that.
回答1:
You have to set the mimetype of the document.
> public void performFileSearch() {
>
> // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
> // browser.
> Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
>
> // Filter to only show results that can be "opened", such as a
> // file (as opposed to a list of contacts or timezones)
> intent.addCategory(Intent.CATEGORY_OPENABLE);
>
> // Filter to show only images, using the image MIME data type.
> // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
> // To search for all documents available via installed storage providers,
> // it would be "*/*".
> intent.setType("image/*");
>
> startActivityForResult(intent, READ_REQUEST_CODE); }
回答2:
These intents use different APIs
ACTION_GET_CONTENT is the older one (since API level 1).
ACTION_OPEN_DOCUMENT_TREE is part of the Storage Access Framework introduced in API level 19. Unfortunately this intent is not supported by any SAF provider I know of, not event Google Drive. As far as I know it is only used to access local storage (
android.content.extra.SHOW_ADVANCED
is an undocumented extra showing the internal storage root)
While not as widely implemented as one would like, the SAF is still usable. You can use :
- ACTION_CREATE_DOCUMENT if you want to save a single document (as opposed to let the user choose a subtree where you can write multiple documents or create subdirectories)
- ACTION_OPEN_DOCUMENT to select an existing document.
See Open files using storage access framework for more details.
回答3:
For Xamarin to select a directory in android devices you can simply use PortableStorage which is one of my project. It uses ACTION_OPEN_DOCUMENT_TREE via SAF and does exactly what you need. https://github.com/madnik7/PortableStorage
来源:https://stackoverflow.com/questions/50980397/when-using-intent-actionopendocumenttree-can-you-allow-the-user-to-save-in-a-loc