1

i am using download manager for downloading a file. In order to show download progress i need file size. When i used the following code , I got file size as -1. How i get correct file size?

DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadmanagerid1);
Cursor cursor = dm.query(q);
if (cursor.moveToFirst()) {

FileDownloading.setDownload_status(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));
long fileSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
long bytesDL = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
Log.e("file size getting ",""+fileSize);    
FileDownloading.setDownload_progress((int) ((bytesDL * 100.0f) / fileSize));

}
else {
FileDownloading.setDownload_progress(0);
FileDownloading.setDownload_status(-1);
}
cursor.close();
Anu
  • 1,303
  • 3
  • 23
  • 38

2 Answers2

1

Sometimes the download link you are trying to download with will not give access to fetch the size and will eventually show the file size to "-1".

I would recommend to try links from https://file-examples.com/ , MP3 and MP4 links are working perfectly.

0

Do like this way :

while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
               // publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                // writing data to file
                progressBar.setProgress((int) ((total * 100) / lenghtOfFile));
                output.write(data, 0, count);
            }
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39