webview实现文件下载功能

WebView默认没有开启文件下载的功能,如果要实现文件下载的功能,需要设置WebView的DownloadListener,通过实现自己的DownloadListener来实现文件的下载。具体操作如下:

  1、设置WebView的DownloadListener:

    webView.setDownloadListener(new MyWebViewDownLoadListener());

  2、实现MyWebViewDownLoadListener这个类,具体可以如下这样:

private class MyWebViewDownLoadListener implements DownloadListener {  
  
        @Override  
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,  
                                    long contentLength) {  
            Uri uri = Uri.parse(url);  
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
            startActivity(intent);  
        }  
  
    }  

这只是调用系统中已经内置的浏览器进行下载,还没有WebView本身进行的文件下载,不过,这也基本上满足我们的应用场景了。

原文地址:https://www.cnblogs.com/bigthing33/p/5166040.html