Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 955
sergiy tikhonov Avatar asked Oct 19 '25 14:10

sergiy tikhonov


1 Answers

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"
like image 104
sergiy tikhonov Avatar answered Oct 21 '25 04:10

sergiy tikhonov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!