3

Hi in my android application i need to download media files from server, for that i have used Download Manager service. And also i want to show the progress of the download in ui. Downloading works perfectly but progress bar is updating only when status becomes STATUS_SUCCESSFUL. I have used the following code for finding the percentage of downloaded data,

DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(enqueue);
                Cursor cursor = dm.query(q);
                cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor                      .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));    
final long dl_progress = (bytes_downloaded*100L)/bytes_total;

i have noticed that the value of bytes_total remain -1 during status STATUS_RUNNING, it is only fills up when download finishes. So how can i update the progressbar with this calculation. please help...

Nidheesh
  • 433
  • 10
  • 20
  • On some devices, the number of bytes remains to -1 which is the initial value according to the [doc](http://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TOTAL_SIZE_BYTES) – david Jul 15 '15 at 10:22

1 Answers1

0

This could be due to server is not supporting http HEAD or OPTION call. I had a similar issue when I tried this with Simple Python server where I got the value for COLUMN_TOTAL_SIZE_BYTES as -1. But when I tried with an apache tomcat this problem was resolved.

Most probably this could happen due to server is not sending necessary meta data. If you are trying this with the Android emulator you can check this by using Wireshark or any other packet monitoring software to see whether the server is supporting the network call done my Android Download Manager.

Mad
  • 435
  • 2
  • 17