我正在尝试从 Firebase 存储中下载一些文件。当有稳定的互联网连接时,它运行良好。但是,如果在下载内容的过程中互联网连接中断,它只会继续尝试下载内容。如何检测是否没有内容正在下载?
我已经实现了 StorageReference
的 onProgessListener
。但是,我不确定如何使用它来检测下载是否没有进展。
new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
//What to do with the taskSnapshot to detect if there are no progress in the download?
}
};
最佳答案
上传、下载和其他操作旨在自动重试并保护您免受临时中断。 如果无法在该时间内传输数据包,则有一个可配置的超时将结束(失败)操作。 您可以通过以下方式进行设置:
//if we get interrupted for more than 2 seconds, fail the operation.
FirebaseStorage.getInstance().setMaxUploadRetryTimeMillis(2000);
上传、下载等操作有不同的超时时间。
对于上传,您可以尝试从中断的地方继续上传(如果您的源是文件),即使它已被中断。为此,您需要从上传任务中获取“上传 session uri”。
task.addOnFailureListener(new OnFailureListener {
@Override
public void OnFailure(Exception error) {
Uri uploadsession = task.getSnapshot().getUploadSessionUri();
//if you want to retry later, feed this uri as the last parameter
// to putFile(uri, metadata, uri)
}
}
关于android - Firebase 存储在下载过程中处理网络中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499262/