2016-02-26 76 views
2

我遇到了DownloadManager奇怪的問題,下載成功但文件沒有存儲。DownloadManager下載完成但文件沒有存儲

所以這是我的代碼:

try { 
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); 
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); 
    request.setAllowedOverRoaming(false); 
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); 
    request.setDestinationInExternalFilesDir(context, /temp/, "test.mp4"); 
    final long downloadId = manager.enqueue(request); 
    boolean downloading = true; 
    while (downloading) { 
     DownloadManager.Query query = new DownloadManager.Query(); 
     query.setFilterById(downloadId); 
     Cursor cursor = manager.query(query); 
     cursor.moveToFirst(); 
     int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); 
     int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
     int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
     if(status==DownloadManager.STATUS_SUCCESSFUL){ 
      Log.i("Progress", "success"); 
      downloading = false; 
     } 
     final int progress = (int) ((bytesDownloaded * 100l)/bytesTotal); 
     cursor.close(); 
     subscriber.onNext(progress); 
    } 
    subscriber.onCompleted(); 
}catch (Exception e){ 
    subscriber.onError(e); 
} 

我已經包含在我的清單WRITE_EXTERNAL_STORAGE了。我嘗試將目錄更改爲Environment.DIRECTORY_DOWNLOADS,但文件仍未存儲到下載目錄。我試圖在/Android/data/<my package>/上找到它,下載的文件也不在那裏。那麼我的代碼有什麼問題?

附加: 在日誌中顯示我的下載已完成。

enter image description here

+0

你在外部存儲器中給出文件夾路徑'/ temp /'。你在那裏檢查? – Rohit5k2

+0

@ Rohit5k2我做了,不在那裏... –

+0

看看http://www.gadgetsaint.com/android/download-manager/#.WSK0Yut96Hs – ASP

回答

0

我有這個問題太,但它解決了,當我改變

request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, videoName+".mp4"); 

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS.toString(), videoName+".mp4"); 

現在保存在 「下載」 文件夾中。

相關問題