2017-05-04 122 views
0

我在android webview中打開一個網頁。填寫網頁上的信息後,會出現一個用於下載文件的按鈕。但該文件沒有下載。我對將哪個url傳遞給下載任務感到困惑。叮噹響按鈕應該下載一個pdf文件。但它什麼也沒做Webview下載文件

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.setMimeType(mimetype); 
       //------------------------COOKIE!!------------------------ 
       String cookies = CookieManager.getInstance().getCookie(url); 
       request.addRequestHeader("cookie", cookies); 
       request.addRequestHeader("User-Agent", userAgent); 
       request.setDescription("Downloading file..."); 

       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,URLUtil.guessFileName(url, contentDisposition, mimetype)); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); 
       dm.enqueue(request); 


      } 
     }); 
+0

您是否設置了正確的權限?分享您的清單文件並將記錄器放入您的代碼並共享日誌 –

+0

您是否設置了正確的權限?分享您的清單文件並將記錄器放入您的代碼並共享日誌 –

+0

<使用權限android:name =「android.permission.INTERNET」/> <使用權限android:name =「android.permission.ACCESS_NETWORK_STATE」/>

回答

0

你試過了嗎?

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

});

+0

不工作,已經嘗試 –

+0

我已經把日誌文件放在下載監聽器中,但它似乎不會進入內部 –