Firebase存储处理下载进行中的网络中断

Abd*_*man 1 android download interruption firebase firebase-storage

我正在尝试从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?
    }
};
Run Code Online (Sandbox Code Playgroud)

Ben*_*lfe 5

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

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

上载,下载和其他操作的超时时间不同。

使用上载,即使中断,您也可以尝试从上次中断的地方继续上载(如果源是文件)。为此,您需要从上传任务中获取“上传会话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)
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 上传/下载的默认超时时间为 10 分钟,其他操作的默认超时时间为 2 分钟(例如,getDownloadUrl) (2认同)