场景:
问题是,我正在做与此类似的事情以能够管理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,因此,这当然不起作用:
相反,它更像是这样:
Web浏览器可以使用这种URL,但Webview不能。
如何使Android Webview将文件下载到设备上?