Home > Software engineering >  How to enable picking Google Docs files (kept on Google Drive) with Intent?
How to enable picking Google Docs files (kept on Google Drive) with Intent?

Time:11-10

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). 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.

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). At the same time I can choose another types of files - for example, PDF, PNG, if I set their MIME-type.

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.

I tried several MIME-types I could find (here and here for example), but all of them haven't fixed problem.

What MIME-type should be used for this? Or maybe problem is not in MIME-type?

CodePudding user response:

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.

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

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:

"application/vnd.google-apps.document"
  • Related