Download PDF file from Blob Url inside Android's WebView

Viewed 831

The scenario:

  • I got a RestFul API that generates pdf files
  • I use this API to download pdf files in Web applications and works great
  • Now I want to open this web application in a Webview in an Android App to download this same files

The problem is, I'm doing something similar to this to be able to manage the Webview downloads:

webview.settings.javaScriptEnabled = true
webview.settings.domStorageEnabled = true

webview.setDownloadListener { url, userAgent, contentDisposition, mimeType, _ ->
    val subpath = "/download" + "/" + "test" + ".pdf"
    val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    val request = DownloadManager.Request(Uri.parse(formatUri(url)))
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
    request.setAllowedOverRoaming(false)
    request.setVisibleInDownloadsUi(true)
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subpath)
    downloadManager.enqueue(request)            
}

But, of course, this will not work since url that returns is not a 'normal' Uri like:

https://test.com/your-pdf-file.pdf

Instead, it is more like it:

blob:https://test.com/d335cd66-b3f2-4fd5-b16f-asbca22dfc38

The web browser is able to work with this kind of url, but the webview is not.

How can I make the Android webview download the files to the device?

0 Answers
Related