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>/
上找到它,下載的文件也不在那裏。那麼我的代碼有什麼問題?
附加: 在日誌中顯示我的下載已完成。
你在外部存儲器中給出文件夾路徑'/ temp /'。你在那裏檢查? – Rohit5k2
@ Rohit5k2我做了,不在那裏... –
看看http://www.gadgetsaint.com/android/download-manager/#.WSK0Yut96Hs – ASP