Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update progress bar with Firebase uploading

I tried to add a progress bar to uploading files on firebase. but unfortunalty it does not indicate upload progress. both logcat & progress bar only indicate when file reached to 100%

   uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = 100.0 * (taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                System.out.println("Upload is " + progress + "% done");
            int currentprogress = (int) progress;
                progressBar.setProgress(currentprogress);
            }
        }).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
                System.out.println("Upload is paused");
            }
        });
like image 457
APP Bird Avatar asked Jul 09 '16 04:07

APP Bird


1 Answers

Change the grouping of terms in your calculation of progress to force conversion to float. As your code is now, you are dividing two longs. The result of the division will be 0 until getBytesTransferred() == getTotalByteCount(), then it will be 1.

 double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
like image 78
Bob Snyder Avatar answered Oct 12 '22 23:10

Bob Snyder