SikhWarrior
SikhWarrior

Reputation: 1087

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. When I check the download history, there is a download.bin file. 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.

Upvotes: 0

Views: 1125

Answers (1)

MV1
MV1

Reputation: 64

Download.bin is the download file. 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).

Upvotes: 1

Related Questions