简体   繁体   中英

showing download progress in progress bar using DownloadManager

I am working on an android app in which i am using DownloadManager to download a file from a server.

Problem

While the file download is in progress, I want to show the download progress via progress bar . File is downloaded successfully but I am not able to show the download progress.

Code I use to show progress

private void startAppDownload() {

    ...

    // code to show download progress
    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean isDownloading = true;
            int downloadStatus, totalBytesDownloaded, totalBytes;

            DownloadManager.Query downloadQuery = new DownloadManager.Query();
            downloadQuery.setFilterById(downloadID);

            Cursor cursor = downloadManager.query(downloadQuery);
            cursor.moveToFirst();

            while (isDownloading) {

                totalBytesDownloaded = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                );

                totalBytes = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                );

                downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

                if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
                    isDownloading = false;
                }

                final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        downloadProgressBar.setProgress(downloadProgress);
                    }
                });
            }

            cursor.close();
        }
    }).start();
}

I logged the totalBytesDownloaded variable but it is always zero and the totalBytes variable is always -1 .

This causes downloadProgress variable to always be zero , hence progress bar shows no progress.

Question

What am I doing wrong here? Why is totalBytesDownloaded variable always equal to zero and totalBytes variable always -1 ?

You have a Logic error, Your Query is out of the while loop where the UI is updated

private void startAppDownload() {


    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean isDownloading = true;
            int downloadStatus, totalBytesDownloaded, totalBytes;



            while (isDownloading) {

                DownloadManager.Query downloadQuery = new DownloadManager.Query();
                downloadQuery.setFilterById(downloadID);

                Cursor cursor = downloadManager.query(downloadQuery);
                cursor.moveToFirst();

                totalBytesDownloaded = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                );

                totalBytes = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                );

                downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

                if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
                    isDownloading = false;
                    break;;
                }

                final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        downloadProgressBar.setProgress(downloadProgress);
                    }
                });
            }

            cursor.close();
        }
    }).start();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM