When using Intent.ActionOpenDocumentTree can you allow the user to save in a location off the device?

邮差的信 提交于 2019-12-24 14:36:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!