2017-05-04 95 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

我已经把日志文件放在下载监听器中,但它似乎不会进入内部 –