2012-04-09 106 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