java - 如何在 android 10 或更高版本的 webview 中下载 .pdf 文件
问题描述
webView = findViewById(R.id.webView);
webView.loadUrl(url);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().supportMultipleWindows();
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setDownloadListener(new DownloadListener()
{
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading File...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}});
`我正在开发一个 webview 应用程序,它在 Android 10 上下载 .bin 文件,我已经在 android 9 上测试了该应用程序,它可以正常工作。我是android开发的新手。
解决方案
推荐阅读
- c# - C# UWP:访问列表视图中的文本块和复选框的状态
- facebook - 对象故事规范格式不正确。可能缺少页面 ID 或广告素材详细信息,或者存在无效字段
- javascript - 在 React 中提交后重定向
- .net - 由于请求大小,.NET WCF Soap 服务不返回响应
- c# - IEnumerable 不包含“长度”的定义
- laravel-5 - 复数标签的语法
- svg - 在传单地图上显示 SVG
- ms-access - 用于更高版本 MS Access 的 Microsoft CommonDialog 控件
- paypal - Paypal 定期付款未收取费用
- jquery - 从另一个网页 jquery 显示模态