2012-04-09 111 views
45

我的Android应用程序中有一个webview。当用户转到webview并点击链接下载文件时,什么都不会发生。在WebView中下载文件

URL = "my url"; 
mWebView = (WebView) findViewById(R.id.webview); 
mWebView.setWebViewClient(new HelloWebViewClient()); 
mWebView.getSettings().setDefaultZoom(ZoomDensity.FAR); 
mWebView.loadUrl(URL); 
Log.v("TheURL", URL); 

如何启用在webview内下载?如果我禁用webview并启用意图从应用程序加载浏览器上的URL,然后下载无缝工作。

String url = "my url"; 
Intent i = new Intent(Intent.ACTION_VIEW); 
i.setData(Uri.parse(url)); 
startActivity(i); 

有人可以帮我吗?该页面加载没有问题,但链接到HTML页面中的图像文件不工作...

+0

你可以使用这个'Webview'子类,可以自动处理下载等:https://github.com/delight-im/Android-AdvancedWebView – caw 2015-01-08 01:50:56

回答

64

你试过了吗?

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); 
    } 
}); 
+1

这只是重新加载HTML网页浏览器。如果附件的网址在浏览器中打开,它会更好。谢谢你的回复。赞赏它。 – 2012-04-09 05:42:35

+0

你能发布你的网址吗? – user370305 2012-04-09 05:51:20

+0

http://demo.rdmsonline.net/incidentinfo.aspx?incid=la%2fMDMOBS4VeBNKk6yNz%2fg%3d%3d这是我的网址 – 2012-04-09 06:06:35

15

尝试使用下载管理器,它可以帮助你下载你想要的一切,节省你的时间。

检查那些选项:

选择1 - >

mWebView.setDownloadListener(new DownloadListener() { 
     public void onDownloadStart(String url, String userAgent, 
       String contentDisposition, String mimetype, 
       long contentLength) { 
Request request = new Request(
          Uri.parse(url)); 
        request.allowScanningByMediaScanner(); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download"); 
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
        dm.enqueue(request);   

     } 
    }); 

选择2 - >

if(mWebview.getUrl().contains(".mp3") { 
Request request = new Request(
         Uri.parse(url)); 
       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download"); 
// You can change the name of the downloads, by changing "download" to everything you want, such as the mWebview title... 
       DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
       dm.enqueue(request);   

    } 
31

尝试了这一点。在经历了很多帖子和论坛之后,我发现了这一点。

mWebView.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.allowScanningByMediaScanner(); 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II "); 
      DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      dm.enqueue(request); 
      Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded 
        Toast.LENGTH_LONG).show(); 

     } 
    }); 

不要忘了给这个权限!这个非常重要!在你的Manifest文件(AndroidManifest.xml文件)中加入这个文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  <!-- for your file, say a pdf to work --> 

希望这会有所帮助。 干杯:)

+8

感谢它的工作!,使用原始文件名,我们可以使用:'最后的字符串文件名= URLUtil.guessFileName(url,contentDisposition,mimetype);' – Jawaad 2015-09-21 20:09:27

+7

@SaiZ你的例子包含一些关于未使用的意图的代码,我想你应该去掉它。 – 2015-10-30 00:09:06

+0

@ J.C我删除未使用的代码,感谢上报;) – MatPag 2017-03-14 17:01:13

7
mwebView.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(); 
}}); 
+0

请解释一下你写什么。只是代码是不够的 – Saveen 2016-12-31 16:27:01

+0

设置cookie保存我的一天!谢谢! – 2018-02-01 19:06:57