2017-04-19 93 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); 
設置過濾器