我正在尝试使用安卓应用程序中的totalSize从Firebase存储中下载该文件的Url。问题是FileDownloadTask.getTotalByteCount()返回-1。这些文件不是决定性的。https://firebase.google.com/docs/reference/android/com/google/firebase/storage/FileDownloadTask.TaskSnapshot.html#getTotalByteCount()
它讨论了在下载任务中上传的字节。我想知道是否有人遇到了这个问题和解决这个问题的方法?
这是片段
for (DataSnapshot child : dataSnapshot.getChildren()) {
String url = child.getValue().toString();
StorageReference filesRef = FirebaseStorage.
getInstance().
getReferenceFromUrl(url);
if (filesRef == null) continue;
final String fileName = filesRef.getName();
final String[] path = filesRef.getPath().split("/");
final String folder = path[path.length - 2];
File dir = new File(
context.
getFilesDir().
getPath() +
File.separator + folder);
if (!dir.exists()) dir.mkdir();
final File localFile = new File(dir, fileName);
FileDownloadTask task = filesRef.getFile(localFile);
task.getSnapshot().getTotalByteCount();
Log.i("ccss", "totalByteCount :: " +
task.getSnapshot().getTotalByteCount());输出量
I/ccss: totalByteCount :: -1
I/ccss: totalByteCount :: -1
I/ccss: totalByteCount :: -1
I/ccss: totalByteCount :: -1我试着将它放入onProgressListener中,有时返回-1,有时是实际大小。有什么解释吗?
发布于 2017-07-22 04:01:26
存储文件及其元数据位于Firebase服务器上。它们的值是异步加载的,在您使用filesRef.getFile(localFile)启动下载时无法立即使用。
FileDownloadTask.TaskSnapshot的文档没有定义getTotalByteCount()值何时有效。考虑到它必须从服务器获取,您可以假设在启动下载时,不是是有效的,并且在下载成功完成之前可能是无效的。
下面的代码在下载的各个点对getTotalByteCount()的值进行了示例。它证实了您的观察,即当下载开始时,getTotalByteCount()是-1,在onProgress()的最初几次调用中也是-1。然后,在随后的onProgress()调用中,它变得有效,当调用onComplete()时,它仍然有效。
要可靠地获得总字节计数,请使用OnCompleteListener或OnSuccessListener,并从回调中返回的FileDownloadTask.TaskSnapshot获取计数。
FileDownloadTask task = filesRef.getFile(localFile);
FileDownloadTask.TaskSnapshot snap = task.getSnapshot();
Log.d(TAG, String.format("atStart: bytes=%d total=%d",
snap.getBytesTransferred(), snap.getTotalByteCount()));
task.addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onProgress(FileDownloadTask.TaskSnapshot snap) {
Log.d(TAG, String.format("onProgress: bytes=%d total=%d",
snap.getBytesTransferred(), snap.getTotalByteCount()));
}
}).addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
long total = task.getResult().getTotalByteCount();
long trans = task.getResult().getBytesTransferred();
Log.d(TAG, String.format("onComplete: bytes=%d total=%d", trans, total));
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: SUCC");
} else {
Log.d(TAG, "onComplete: FAIL " + task.getException().getMessage());
}
}
});示例输出:
atStart: bytes=0 total=-1
onProgress: bytes=0 total=-1
onProgress: bytes=0 total=-1
onProgress: bytes=262144 total=2814800
onProgress: bytes=524288 total=2814800
onProgress: bytes=786432 total=2814800
onProgress: bytes=1048576 total=2814800
onProgress: bytes=1310720 total=2814800
onProgress: bytes=1572864 total=2814800
onProgress: bytes=1835008 total=2814800
onProgress: bytes=2097152 total=2814800
onProgress: bytes=2359296 total=2814800
onProgress: bytes=2621440 total=2814800
onProgress: bytes=2814800 total=2814800
onComplete: bytes=2814800 total=2814800
onComplete: SUCChttps://stackoverflow.com/questions/45245225
复制相似问题