Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android 10或更高版本的webview中下载.pdf文件_Java_Android_Android Studio_Webview_Android Download Manager - Fatal编程技术网

Java 如何在android 10或更高版本的webview中下载.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应用程序,它在安卓10上下载.bin文件,我在安卓9上测试了该应用程序,它正常工作。我是android开发新手。

您可以使用HTTP客户端API(如OkHttp)下载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();
            }});