2017-07-19 23 views
-1

我有以下问题:如何在不知道文件名的情况下将我的WebView的下载目标设置为Downloads文件夹?

我有一个WebView。这WebView有一个下载监听器,当用户正在尝试下载文件时动作。

我想要将文件下载到常规“下载”文件夹。我可以用

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title);

这样做,但我怎么保存在其原始名称的文件?

我不能使用

String name = URLUtil.guessFileName(url, null, mimetype); 

,因为这是被称为不包含文件名的URL。

我的下载管理器目前看起来是这样的:

mainWebView.setDownloadListener(new DownloadListener() { 
     public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { 
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
      // Show a download notification 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
      DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      String title = URLUtil.guessFileName(url, null, mimetype); 

      // Set directory of where the file should be saved to 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title); 
      // Start the download 
      dm.enqueue(request); 
     } 
} 

注:我目前使用的URLUtil.guessFilename()方法,因为这将让我救我的文件,事件如果是下错了名字。

回答

0

SOLUTION

将溶液通过适当的contentDispositionURLUtil.guessFileName()功能。

获取标题是这样的:

String title = URLUtil.guessFileName(url, contentDisposition, mimetype); 

(所有参数传递到public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)

相关问题