【问题标题】:How to enable picking Google Docs files (kept on Google Drive) with Intent?如何启用 Intent 选择 Google Docs 文件(保存在 Google Drive 上)?
【发布时间】:2020-09-17 14:14:50
【问题描述】:

我想在我的 Android 应用程序中选择保存在 Google Drive 上的 Google Docs 文件,然后 - 从中​​读取一些数据(但这超出了这个问题的范围)。所以一开始我只想点击那个文件并得到一些回调。我在 Google Drive Android API Deprecation 上关注 this recommendations 并迁移到 Google Drive Rest API

我设法与 Google Drive 的文件列表对话,但我无法选择 Google Docs 文件(我可以看到它们,但它们无法点击它们)。同时,如果我设置了它们的 MIME 类型,我可以选择其他类型的文件 - 例如 PDF、PNG。

现在我使用这段代码:

val pickerIntent = Intent(Intent.ACTION_OPEN_DOCUMENT)
pickerIntent.addCategory(Intent.CATEGORY_OPENABLE)
pickerIntent.type = "*/*"
val mimeTypes = arrayOf(
     "application/pdf", // works
     "image/png", //works
     // none of MIME below works
     "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
     "application/vnd.google-apps.document",
     "application/vnd.google-apps.file"
)
pickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)

official sample 中,他们以 MIME "text/plain" 为例,所以对我没有帮助。

我尝试了几种可以找到的 MIME 类型(例如 herehere),但都没有解决问题。

应该为此使用什么 MIME 类型?或者问题可能不在 MIME 类型中?

【问题讨论】:

    标签: android google-drive-api google-docs google-drive-android-api


    【解决方案1】:

    我在this official documentation's article 中发现了问题的根源。

    问题是 Google Doc 文件 - 与“真实”PDF|PNG 文件不同,它们是所谓的“虚拟”文件,因为它们没有二进制表示。

    我的问题的明确答案是文档中的下一个注释:

    注意:由于应用无法通过 openInputStream() 方法直接打开虚拟文件,因此在创建包含 ACTION_OPEN_DOCUMENT 或 ACTION_OPEN_DOCUMENT_TREE 操作的 Intent 时,请勿使用 CATEGORY_OPENABLE 类别

    所以删除下一行后问题就解决了:

    pickerIntent.addCategory(Intent.CATEGORY_OPENABLE)
    

    此外,我承认,Google Doc 的选择器过滤器适用于下一个 MIME:

    "application/vnd.google-apps.document"
    

    【讨论】:

    • 我也有类似的问题。但这个解决方案对我不起作用。你能看看this question吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多