我试图添加一个进度条来上传firebase上的文件.但不幸的是,它并不表示上传进度. logcat&进度条仅指示文件何时达到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"); } });
解决方法
更改进度计算中的术语分组以强制转换为浮动.正如你的代码现在,你正在划分两个长.除非getBytesTransferred()== getTotalByteCount(),否则除法的结果将为0,然后它将为1.
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();