我正在尝试使用API11 +中的DownloadManager下载文件。到目前为止,文件下载已经很好,但操作栏通知显示未确定大小的进度条,查询轮询返回没有大小,直到文件完全下载。
代码:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(attachment.getUrl())
.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setMimeType(attachment.getContentType())
.setAllowedOverRoaming(true)
.setTitle(attachment.getName())
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
attachment.getName())
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.addRequestHeader("Content-Length", attachment.getSizeInBytes() + "");
long lastDownload = mDownloadManager.enqueue(request);
我的查询是
double progress = 0.0;
query.setFilterById(downloadId);
Cursor cursor = mDownloadManager.query(query);
if (cursor.moveToFirst()) {
int sizeIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
int downloadedIndex = cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
long size = cursor.getInt(sizeIndex);
long downloaded = cursor.getInt(downloadedIndex);
Log.e("THINGS", "Downloaded " + downloaded + " of a total of " + size);
if (size != -1) {
progress = downloaded * 100.0 / size;
}
}
该块中的日志语句返回
// Downloaded 4127 of a total of -1
// Downloaded 842903 of a total of -1
// Downloaded 1440759 of a total of 1440759
,通知与此类似
任何人都可以指出我做错了什么,或者问题是否存在于服务器上?