Get total bytes return -1 on Android Download Manager

Viewed 946

I am creating a progress bar to show percentage download apk in Android download manager but DownloadManager.COLUMN_TOTAL_SIZE_BYTES always returns -1 and when download finished, it returns the file size.

String url = Config.GET_APK+ finalApk;
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.addRequestHeader("Authorization", token);

// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, finalApk);
Log.d("PATH1", String.valueOf(Environment.DIRECTORY_DOWNLOADS + "/" + finalApk));
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downId = manager.enqueue(request);
Log.d("DOWNLOADID", String.valueOf(downId));

download.show();

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean downloading = true;
                while (downloading) {
                    DownloadManager.Query q = new DownloadManager.Query();
                    q.setFilterById(downId);
                    Cursor c = manager.query(q);
                    c.moveToFirst();

                    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    if (status == DownloadManager.STATUS_SUCCESSFUL) {
                        downloading = false;
                        download.dismiss();
                    }
                    final int bytes_download = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                    final int bytes_total = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                    Log.d("bytes_total", String.valueOf(bytes_total));
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            final double dl_prog = ((bytes_download * 100.0) / bytes_total);
                            download.setProgress((int) dl_prog);
                        }
                    });
                    c.close();
                }
            }
        }).start();
    }
},1000);

I already tried solution in this using post delayed + in download manager total size bytes doc and it says it will return -1 until download begins then it will return the download size.

1 Answers

in my case the server was not sending the content length of the file, I checked it with a simple

URL url = new URL("URL");
url.openConnection().getContentLength();

and even here the content length was -1.

After fixing it on the server side, the DownloadManager was returning the correct COLUMN_TOTAL_SIZE_BYTES

Related