简体   繁体   English

如何启用 Intent 选择 Google Docs 文件(保存在 Google Drive 上)?

[英]How to enable picking Google Docs files (kept on Google Drive) with Intent?

I want in my Android app to choose Google Docs file that is kept on Google Drive and after that - to read some data from it (but it's out of this question's scope).我想在我的 Android 应用程序中选择保存在 Google Drive 上的 Google Docs 文件,然后从中读取一些数据(但这超出了这个问题的范围)。 So for the start I just want to click on that file and get some callback.所以一开始我只想点击那个文件并得到一些回调。 I follow this recommendations on Google Drive Android API Deprecation and migration to Google Drive Rest API .我在 Google 云端硬盘 Android API 弃用和迁移到Google 云端硬盘 Rest ZDB974238714CA8DE634A 7 上遵循此建议

I managed to get dialog with list of Google Drive's files, but I can't choose Google Docs files (I can see them but they are not active to click on them).我设法与 Google Drive 的文件列表对话,但我无法选择 Google Docs 文件(我可以看到它们,但它们无法点击它们)。 At the same time I can choose another types of files - for example, PDF, PNG, if I set their MIME-type.同时我可以选择其他类型的文件 - 例如,PDF,PNG,如果我设置它们的 MIME 类型。

For now I use this piece of code:现在我使用这段代码:

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)

In the official sample they used MIME "text/plain" as an example, so it's of no help to me.官方示例中,他们使用 MIME "text/plain" 作为示例,因此对我没有帮助。

I tried several MIME-types I could find ( here and here for example), but all of them haven't fixed problem.我尝试了几种我能找到的 MIME 类型(例如 这里这里),但它们都没有解决问题。

What MIME-type should be used for this?应该为此使用什么 MIME 类型? Or maybe problem is not in MIME-type?或者问题可能不在 MIME 类型中?

I found what was a source of problem in this official documentation's article .我在这篇官方文档的文章中发现了问题的根源。

The thing is Google Doc files - are not the same as "real" PDF|PNG files, they are so-called "virtual" files, since they don't have a binary representation.问题是 Google Doc 文件 - 与“真实”PDF|PNG 文件不同,它们是所谓的“虚拟”文件,因为它们没有二进制表示。

The clear answer to my problem is the next note in documentation:我的问题的明确答案是文档中的下一个注释:

Note: Because an app cannot directly open a virtual file by using the openInputStream() method, don't use the CATEGORY_OPENABLE category when creating the intent that contains the ACTION_OPEN_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE action注意:由于应用程序无法通过 openInputStream() 方法直接打开虚拟文件,因此在创建包含 ACTION_OPEN_DOCUMENT 或 ACTION_OPEN_DOCUMENT_TREE 操作的 Intent 时,请勿使用 CATEGORY_OPENABLE 类别

So problem was solved after I removed next line:所以在我删除下一行后问题就解决了:

pickerIntent.addCategory(Intent.CATEGORY_OPENABLE)

In addition I can admit, that picker's filter for Google Doc works well with the next MIME:此外,我可以承认,Google Doc 的选择器过滤器适用于下一个 MIME:

"application/vnd.google-apps.document"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM