Firebase存储上传进度不准确 [英] Firebase storage upload progress inaccurate
问题描述
onProgress函数存在问题,被addOnProgressListener覆盖.
There is a problem with the onProgress function that is overrided by addOnProgressListener.
我的问题是,当我尝试上传图像时,TaskSnapshot不会返回已传输的字节.它只是保持为0.这是我为此的一小段代码:
My problem is that the TaskSnapshot when I try to upload an image does not return the bytes that have been transferred. It just stays at 0. Here is a snippet of code that I have for this:
StorageReference myStorageRef = momentsStorageRef.child(momentID + ".jpeg");
UploadTask uploadTask = myStorageRef.putBytes(data, new StorageMetadata.Builder()
.setContentType("image/jpeg")
.build());
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
int bytesTransferred = (int) taskSnapshot.getBytesTransferred();
int totalBytes = (int) taskSnapshot.getTotalByteCount();
int progress = (100 * bytesTransferred) / totalBytes ;
Log.v(TAG, "Bytes transferred: " + taskSnapshot.getBytesTransferred());
Log.v(TAG, "TotalBytes: " + totalBytes);
Log.v(TAG, "Upload is: " + progress + "% done");
mBuilder.setProgress(100, progress, false);
mNotifyManager.notify(APPLICATION_NOTIFICATION_ID, mBuilder.build());
}
})
这是logCat:
05-28 19:21:33.911 27673-27673:传输的字节数:0
05-28 19:21:33.911 27673-27673:总字节数:205846
05-28 19:21:33.911 27673-27673:上传为:0%完成
05-28 19:21:35.637 27673-27673:字节 转移:0
05-28 19:21:35.637 27673-27673:TotalBytes:205846
05-28 19:21:35.637 27673-27673:上传为:已完成0%
05-28 19:21:41.458 27673-27673传输的字节数:205846
05-28 19:21:41.458 27673-27673 TotalBytes:205846
05-28 19:21:41.458 27673-27673:上传为:100% 完成
05-28 19:21:33.911 27673-27673: Bytes transferred: 0
05-28 19:21:33.911 27673-27673: TotalBytes: 205846
05-28 19:21:33.911 27673-27673: Upload is: 0% done
05-28 19:21:35.637 27673-27673: Bytes transferred: 0
05-28 19:21:35.637 27673-27673: TotalBytes: 205846
05-28 19:21:35.637 27673-27673: Upload is: 0% done
05-28 19:21:41.458 27673-27673 Bytes transferred: 205846
05-28 19:21:41.458 27673-27673 TotalBytes: 205846
05-28 19:21:41.458 27673-27673: Upload is: 100% done
推荐答案
firebaser此处
以256KB的块为单位来衡量进度.由于您的文件小于该文件,因此它可以容纳一整块,因此进度一口气就从0%跃升到100%.
The progress is measured in chunks of 256KB. Since your file is smaller than that, it fits in one chunk and progress thus jumps from 0% to 100% in one go.
在文件较小和带宽较低的情况下,我们有一个开放的任务来提高进度测量的粒度.
We have an open task to improve the granularity of the progress measurement in the case of smaller files and lower bandwidth connections.
这篇关于Firebase存储上传进度不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!