简体   繁体   English

Android WebView下载文件没有任何作用

[英]Android WebView download file does nothing

I'm currently trying to download a file through the webview but seem to be getting no luck. 我目前正在尝试通过网络视图下载文件,但似乎没有运气。 I am using the download manager, and stepping through the code it receives all the proper information in the webview download listener, but when starting the download, the notification appears for a brief instant, and then vanishes. 我正在使用下载管理器,并逐步执行代码以在Webview下载侦听器中接收所有正确的信息,但是开始下载时,通知会短暂显示,然后消失。 When I check the download history, there is a download.bin file. 当我检查下载历史记录时,有一个download.bin文件。 Any reason why this is happening? 为什么会这样呢? Code below. 下面的代码。

webView = (WebView) findViewById(R.id.wv_webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (login) {
                webView.loadUrl("javascript:(function() { "
                        + "document.getElementsByName(\"username\")[0].value = \"" + computingid + "\";" +
                        "document.getElementsByName(\"password\")[0].value = \"" + password + "\"; " +
                        "document.forms[0].submit();"
                        + "})()");
                login = false;
            }
        }
    });
    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Log.d("TEST", "Test");
            Uri source = Uri.parse(url);
            final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(source);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"Test");
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            manager.enqueue(request);
        }
    });

I'm not seeing any errors in logcat, so I'm not sure how to debug this. 我在logcat中没有看到任何错误,所以我不确定如何调试它。

Download.bin is the download file. Download.bin是下载文件。 You need to either rename it with the correct filename or use HttpURLConnection on the download link and get the correct filename from the headers (content-disposition). 您需要使用正确的文件名对其进行重命名,或者在下载链接上使用HttpURLConnection并从标题(内容处置)中获取正确的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM