如何在Android中通过webView下载文件

3

我想在Android的webView中下载一个文件,但是我不知道如何做? webView的代码如下。你能否建议我在webView中下载文件应该怎么做?

 wv = (WebView) findViewById(R.id.mainwebview);
        WebSettings ws = wv.getSettings();
     //   ws.setBuiltInZoomControls(true);
    //     wv.loadUrl("http://www.ebooksdownloadfree.com/");
        wv.getSettings().setJavaScriptEnabled(true);


        this.wv.getSettings().setSupportZoom(false);
        this.wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        this.wv.loadUrl("http://www.ziddu.com/download/20520057/MPTOH.pdf.html");
     wv.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
          Intent i = new Intent(Intent.ACTION_VIEW);
          i.setData(Uri.parse(url));
          startActivity(i);
        }
    });

运行此代码后,我得到了如下图片所示的错误: 这里输入图片描述
1个回答

1

我认为这个链接对你有帮助。它是关于在Android的webview中下载文件的。

编辑:

请尝试以下代码。

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype,
            long contentLength) {
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(url));
      startActivity(i);
    }
});

我无法理解那个答案中提供的代码。你能把那段代码与我的代码集成吗?这对我来说会非常有帮助…… - osimer pothe
是的,我已经上传了这个文件。 - osimer pothe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接