从Android的WebView中的Blob Url下载PDF文件

时间:2018-09-11 19:50:29

标签: java android webview kotlin

场景:

  • 我有一个可生成pdf文件的RestFul API
  • 我使用此API在Web应用程序中下载pdf文件,效果很好
  • 现在,我想在Android应用程序的Webview中打开此Web应用程序以下载相同的文件

问题是,我正在做与此类似的事情以能够管理Webview下载:

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)            
}

但是,由于返回的 url 不是像下面这样的“正常” Uri,因此,这当然不起作用:

  

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

相反,它更像是这样:

  

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

Web浏览器可以使用这种URL,但Webview不能。

如何使Android Webview将文件下载到设备上?

0 个答案:

没有答案