Update progress bar with Firebase loading

I tried to add a progress bar to upload files to firebase. but failure does not indicate download progress. both logcat and the progress bar only indicate when the file has reached 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");
            }
        });
+10
source share
2 answers

Change the grouping of terms in your calculation progressto force conversion to float. Since your code is now, you are sharing two long ones. The result of division will be 0 to getBytesTransferred() == getTotalByteCount(), then it will be 1.

 double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
+23
source

When you send data to the repository, you can watch what you download.

let uploadTask = profileRef.putData(data, metadata: metadata) { (metadata, error) in
     if let error = error {
              // Awesome error handling 
     }
}

, , . , , , _. 0 1 . , , , .

task.observe(.progress, handler: { (snap) in
   print("Our upload progress is: \(String(describing: snap.progress?.fractionCompleted))")
})
0

All Articles