由 addOnProgressListener 覆盖的 onProgress 函数存在问题。
我的问题是我尝试上传图像时的 TaskSnapshot 没有返回已传输的字节。它只是保持在 0。这是我为此提供的一段代码:
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:总字节数: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 总字节数:205846
05-28 19:21:41.458 27673-27673:上传完成:100%