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!