2017-04-19 98 views
0

我正在为我的应用程序使用Android下载管理器,并且工作完美。但是我需要在存储时下载文件名。我没有得到文件名,并且出现错误。我也尝试了以前的所有解决方案。请帮我解决这个问题。无法从Android下载管理器获取下载文件的名称

 DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE); 
       boolean isDownloading = false; 
       DownloadManager.Query query = new DownloadManager.Query(); 
       query.setFilterByStatus(
         DownloadManager.STATUS_PAUSED| 
           DownloadManager.STATUS_PENDING| 
           DownloadManager.STATUS_RUNNING| 
           DownloadManager.STATUS_SUCCESSFUL 
       ); 
       Cursor cur = mgr.query(query); 
    String filename=""; 

       if (cur.moveToFirst()) { 
String filePath = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); 
     filename = filePath.substring(filePath.lastIndexOf('/')+1, filePath.length()); 
       } 



        Uri source = Uri.parse(myWebsites[j]); 


        DownloadManager.Request request = new DownloadManager.Request(source); 

        request.setNotificationVisibility(
          DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED 
        ); 


        request.setDestinationInExternalPublicDir("/MyDownloadManager/"+folderName,     filename); 

        request.allowScanningByMediaScanner(); 


        long id = mgr.enqueue(request); 

       cur.close(); 

回答

0

这可以正常工作,如果你通过downloadId

query.setFilterById(id); 
设置过滤器