2

I am using an a webview in an Android application. When the user clicks a download link, I am catching the request (shouldOverrideUrlLoading) and performing a download via API request - as the download is only possible when the user is authenticated. Therefore I am using the code below. Somehow many files are not opened correctly, the Quick View shows the error message "Problem with file" (screenshot attached).

The URL that is set to i.data works perfectly fine (authentication and download also works in private mode of any browser)

override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        if (url.indexOf("example.com") !== -1 && (url.indexOf(".pdf") !== -1 || url.indexOf(".xlsx") !== -1)) {
            println("trying to override url loading")
            val i = Intent(Intent.ACTION_QUICK_VIEW)
            val preferences = contextOfApplication!!.getSharedPreferences("example", Context.MODE_PRIVATE)
            val email = preferences.getString("email", "")
            val token = preferences.getString("token", "")
            i.data = Uri.parse("http://example.com/api/getfile/" + url.substring(url.lastIndexOf('/')+1) + "?mail=" + URLEncoder.encode(email, "UTF-8") + "&token=" + URLEncoder.encode(token, "UTF-8") + "&relative_path=" + URLEncoder.encode(URL(url).path))
            contextOfActivity!!.startActivity(i)
            return true
        }
        return false
    }

The url could look like this:

http://example.com/api/getfile/example.pdf?mail=xyz&token=xyz&relative_path=example.pdf

I added the file name (rewritten by server) to the end of the URL, before the Get parameters, so that Android can detect the correct file extension.

Maybe the ACTION_QUICK_VIEW does not support URL's containing GET parameters? Is it possible to bypass this error or to temporarily download the file and open it afterwards?

Thanks in advance!

error message

Dion
  • 3,145
  • 20
  • 37

1 Answers1

2

I would interpret the documentation as meaning that the Uri needs to be a content Uri.

Also, http is banned by default on newer versions of Android (compared to https), so even if a Web URL is supported, the http scheme might not.

You may need to download the content yourself, then use FileProvider to make a content Uri for use with your Intent.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your answer. I will try this and let you know if it worked! – Dion Aug 28 '19 at 14:04
  • The problem actually got fixed by switching to https! The dev environment, I was testing with, didn't have a cert until now. – Dion Sep 11 '19 at 15:38