Java 如何在android 10或更高版本的webview中下载.pdf文件
`我正在开发一个webview应用程序,它在安卓10上下载.bin文件,我在安卓9上测试了该应用程序,它正常工作。我是android开发新手。您可以使用HTTP客户端API(如OkHttp)下载PDF。Java 如何在android 10或更高版本的webview中下载.pdf文件,java,android,android-studio,webview,android-download-manager,Java,Android,Android Studio,Webview,Android Download Manager,`我正在开发一个webview应用程序,它在安卓10上下载.bin文件,我在安卓9上测试了该应用程序,它正常工作。我是android开发新手。您可以使用HTTP客户端API(如OkHttp)下载PDF。 webView = findViewById(R.id.webView); webView.loadUrl(url); webView.getSettings().setAllowFileAccess(true); webView.getSett
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();
}});