3

我正在尝试从 firebase 存储中下载一些文件。当有稳定的互联网连接时,它工作得很好。但是,如果在下载内容中途断开互联网连接,它只会继续尝试下载内容。如何检测是否没有下载内容?

我已经实施onProgessListenerStorageReference. 但是,我不确定如何使用它来检测下载是否没有进度。

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?
    }
};
4

1 回答 1

8

上传、下载和其他操作旨在自动重试并保护您免受临时中断。如果在该时间内无法传输数据包,则有一个可配置的超时将结束(失败)操作。您可以使用以下方法进行设置:

//if we get interrupted for more than 2 seconds, fail the operation.
FirebaseStorage.getInstance().setMaxUploadRetryTimeMillis(2000);

上传、下载等操作有不同的超时时间。

通过上传,您可以尝试从上次中断的地方继续上传(如果您的源是文件),即使上传被中断。为此,您需要从上传任务中获取“上传会话 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)
  }
}
于 2016-06-14T03:04:24.113 回答