I am trying to download files using the DownloadManager from API11+. So far file downloading has gone fine, but the action bar notification displays an undetermined size progress bar and query polling return no size until the file is completely downloaded.
Code:
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);
and my query is
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;
}
}
The log statement in that block returns
// Downloaded 4127 of a total of -1
// Downloaded 842903 of a total of -1
// Downloaded 1440759 of a total of 1440759
and the notification is similar to this
Can anyone point out what am I doing wrong, or if the problem resides on the server?