Problems displaying Google Docs previews in Android app using Documents List API v3.0

Hey everyone,

I’m having trouble showing Google Docs previews in my Android app. I’m using the Documents List API v3.0 and trying to get the docs to show up in a WebView. Here’s what’s happening:

  1. I receive an embed URL from the API.
  2. I set up the WebView with the proper credentials.
  3. When I load the URL, a strange mobile view of the document appears instead of the normal preview.
  4. The buttons on this view trigger 401 errors when tapped.

I tried switching the user agent to a desktop mode, but then the document appears too small and zooming is ineffective.

I really need a neat mobile view without the extra browser controls. Can anyone suggest a fix?

Here’s a sample code snippet I’m using:

val webView = findViewById<WebView>(R.id.preview_web_view)
webView.settings.javaScriptEnabled = true
webView.webViewClient = object : WebViewClient() {
    override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
        // Add authorization headers here
    }
}
webView.loadUrl(embedUrl)

Thanks for your help!

I’ve dealt with this exact problem in one of my projects. What finally worked for me was using the Google Drive REST API v3 instead of the older Documents List API. It’s more reliable and provides better support for mobile views.

Here’s what I did:

  1. Switched to the Drive API v3
  2. Used the ‘files.get’ method to fetch the file metadata
  3. Retrieved the ‘webViewLink’ from the response
  4. Loaded that link in the WebView

This approach gave me a much cleaner preview without the extra controls. Just make sure you’re using the right scopes for authentication.

Also, I found that setting the WebView to load in desktop mode actually helped:

webView.settings.userAgentString = ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36’

This might seem counterintuitive, but it worked better for document previews in my experience. Give it a shot and see if it solves your issue!

hey man, i had this issue too. try using the google drive android api instead of the documents list api. it’s newer and worked better for me. also, make sure you’re using the right scopes for authorization. that fixed the 401 errors i was getting. good luck!

I encountered a similar issue when working with Google Docs previews in my Android app. The problem often stems from the API’s handling of mobile views. One solution that worked for me was to use the exportLinks field instead of the embedUrl.

When fetching the document metadata, look for the ‘exportLinks’ property. It contains URLs for various formats. Try using the ‘application/pdf’ link, which typically provides a cleaner view on mobile devices.

You’ll need to adjust your WebView settings slightly:

webView.settings.loadWithOverviewMode = true
webView.settings.useWideViewPort = true

This approach bypasses the mobile-specific view and gives you more control over the display. It might require additional work to handle different document types, but it solved the preview issues in my case.