Вопрос

I am hoping that this is going to be a silly thing that I am missing, but I have been banging my head against the keyboard trying to figure out where I am going wrong.

I am trying to update a ProgressBar from a DownloadManager in a new Thread. This is working correctly until around halfway, where the ProgressBar resets back to the beginning. From putting in some debug code, I have isolated the issue to this line:

final int dl_progress = (bytes_downloaded*100)/bytes_total;

dl_progress is turning negative halfway through the file downloading! Relevant code block and log output below:

    @Override
    public void run() {

        boolean downloading = true;

        while (downloading) {

            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(downloadId);

            Cursor cursor = manager.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));

            if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                downloading = false;
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        mProgressBar.setVisibility(View.INVISIBLE);
                    }
                });
            }

            final int dl_progress = (bytes_downloaded*100)/bytes_total;

            Log.d("Download", bytes_downloaded + " of " + bytes_total + " (" + dl_progress + "%)");

            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mProgressBar.setProgress((int) dl_progress);
                }
            });
            cursor.close();
        }

    }

And here's the debug:

D/Download(18228): 7614 of 38577287 (0%)
D/Download(18228): 4226950 of 38577287 (10%)
D/Download(18228): 8578734 of 38577287 (22%)
D/Download(18228): 13207130 of 38577287 (34%)
D/Download(18228): 16539590 of 38577287 (42%)
D/Download(18228): 22287422 of 38577287 (-53%)
D/Download(18228): 28363958 of 38577287 (-37%)
D/Download(18228): 32550806 of 38577287 (-26%)
D/Download(18228): 38577287 of 38577287 (-11%)

I'm certain it's me doing something silly, but I cannot see the wood for the trees in this case, can anybody shed any light?

Thank you kindly.

Это было полезно?

Решение 2

The intermediate result of bytes_downloaded*100 overflows the supported range of an int (> 2^31-1) and gives you a negative result.

You can solve it by using a long and cast the final result back to an int:

final int dl_progress = (int)((bytes_downloaded*100L)/bytes_total);

Другие советы

Your values exceed maximum value of Integer when you multiply them by 100. Try using double instead on ints

change dl_progress from int to long

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top