android - ACTION_GET_CONTENT 和 ACTION_OPEN_DOCUMENT 之间的真正区别是什么?

标签 android

我很难理解 ACTION_OPEN_DOCUMENTACTION_GET_CONTENT Intent 用于打开可打开文档时之间的区别。如果我在 KitKat 之前支持 Andriod,它不支持 ACTION_OPEN_DOCUMENT,我应该选择 ACTION_GET_CONTENT 吗?

documentation是这样说的:

ACTION_OPEN_DOCUMENT is not intended to be a replacement for ACTION_GET_CONTENT. The one you should use depends on the needs of your app:

  • Use ACTION_GET_CONTENT if you want your app to simply read/import data. With this approach, the app imports a copy of the data, such as an image file.
  • Use ACTION_OPEN_DOCUMENT if you want your app to have long term, persistent access to documents owned by a document provider. An example would be a photo-editing app that lets users edit images stored in a document provider.

ACTION_GET_CONTENT 不也使用 KitKat 中的文档提供程序吗?什么会阻止我拥有“长期、持久的访问权限”?这究竟意味着什么?

基本上,以下两个 fragment 有什么区别?

ACTION_GET_CONTENT

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");

ACTION_OPEN_DOCUMENT

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");

最佳答案

Doesn't ACTION_GET_CONTENT also use document providers in KitKat?

不一定。这取决于发布内容的应用程序的实现。另请注意,DocumentProviderContentProvider 的特定类型。

What would prevent me from having "long term, persistent access"

您从 ACTION_GET_CONTENT 返回的 Uri 可能会为您的应用授予临时权限,以便能够读取和/或写入内容。该授权最终将失效(例如,当您的流程终止时)。因此,例如,将 Uri 保存为数据库中的字符串可能毫无意义。

存储访问框架的一部分包含以下概念:内容提供者可以提供可以持续较长时间(“长期、持久”)的权限授予。虽然没有什么可以阻止应用在 API 级别 19+ 上使用 ACTION_GET_CONTENT 提供此类持久性权限,但使用 ACTION_OPEN_DOCUMENT 会更常见。

Basically, what is the difference between the following two snippets?

用户体验会有所不同,因为 ACTION_OPEN_DOCUMENT 提供了标准化的文件浏览器风格的界面,而 ACTION_GET_CONTENT 是传统的选择器对话框,然后是一些特定于应用程序的对话框用户界面。

从您作为该内容的消费者的角度来看,ACTION_GET_CONTENT 是如果您现在想使用该内容; ACTION_OPEN_DOCUMENT 是您现在和以后要使用的内容。

编辑:文档链接:

ACTION_OPEN_DOCUMENT

ACTION_GET_CONTENT

来自 Common Intents example for opening a specific type of file :

Instead of retrieving a copy of a file that you must import to your app (by using the ACTION_GET_CONTENT action), when running on Android 4.4 or higher, you can instead request to open a file that's managed by another app by using the ACTION_OPEN_DOCUMENT action and specifying a MIME type. To also allow the user to instead create a new document that your app can write to, use the ACTION_CREATE_DOCUMENT action instead. For example, instead of selecting from existing PDF documents, the ACTION_CREATE_DOCUMENT intent allows users to select where they'd like to create a new document (within another app that manages the document's storage)—your app then receives the URI location of where it can write the new document.

Whereas the intent delivered to your onActivityResult() method from the ACTION_GET_CONTENT action may return a URI of any type, the result intent from ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT always specify the chosen file as a content: URI that's backed by a DocumentsProvider. You can open the file with openFileDescriptor() and query its details using columns from DocumentsContract.Document.

关于android - ACTION_GET_CONTENT 和 ACTION_OPEN_DOCUMENT 之间的真正区别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36182134/

相关文章:

android - 没有开放 Activity paypal

android - kotlinCompilerVersion 已弃用

android - 如何在 Android 上使用 Kotlin 将 FrameLayout 替换为 fragment

java - '使用操作系统独立路径 project.properties 找到多个文件' android 中的错误

android - 图层列表中可绘制的图像按钮?

java - Android 模拟器中的新地理修复中的位置未更新

android - 如何在中国推送通知?

java - 客户端收到 500(GoogleJsonResponseException),尽管应用引擎端点服务器似乎工作正常

java - 使用 Appium 在 Android native 应用程序中滚动的问题

android - 如何在启动 React Native Activity 时发送数据?