Firebase - 在互联网离线时上传图片 [英] Firebase - uploading images when internet is offline

查看:125
本文介绍了Firebase - 在互联网离线时上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

firebase有很好的选择,即使你离线,使用他们的数据库和发送数据到他们的数据库,然后当连接重新启动,它自动发送数据到数据库。



是否可以在Firebase存储中执行?发送图像,即使互联网是离线的,然后当互联网再次打开,它会自动发送图像文件?

如果是这样,我该怎么办呢?如果没有使用firebase,还有其他的选择吗?

感谢

解决方案

是。 Firebase存储客户端支持恢复上传。
请参阅上传的Firebase存储文档( iOS 网络 Android )。





  uploadTask = mStorageRef.putFile(localFile); 
sessionUri = uploadTask.getUploadSessionUri();
//将sessionUri保存到持久存储中以防进程死亡。

然后继续:

  //当进程停止时,从停止的地方恢复上传任务。 
//做这个,传递sessionUri作为最后一个参数$ b $ uploadTask = mStorageRef.putFile(localFile,
new StorageMetadata.Builder()。build(),sessionUri);



更新(20160809)



在创建 uploadTask 时处理sessionUri:


  1. ,获取 sessionUri 并将其存储到应用程序的 SharedPreferences

  2. c> uploadTask 完成后,从应用程序的 SharedPreferences

  3. 中删除sessionUri。如果 SharedPreferences 中存在 sessionUri 。如果是这样:恢复上传。


firebase has great option of using their database and sending data to their db even if you are offline, and then when the connection is up again, it sends automaticlly the data to the db.

is it also possible to do it with the firebase storage? send images even if the internet is offline, and then when the internet is on again, it will send the images files automaticlly?

if so, how can I do it? if not with firebase, any other option?

thanks

解决方案

Yes. The Firebase Storage client supports resuming uploads. See the Firebase Storage documentation for uploads (iOS, Web, Android).

From there for Android:

uploadTask = mStorageRef.putFile(localFile);
sessionUri = uploadTask.getUploadSessionUri();
//save the sessionUri to persistent storage in case the process dies.

And then to resume:

//resume the upload task from where it left off when the process died.
//to do this, pass the sessionUri as the last parameter
uploadTask = mStorageRef.putFile(localFile,
                new StorageMetadata.Builder().build(), sessionUri);

Update (20160809)

One way to handle the sessionUri:

  1. when you create the uploadTask, get the sessionUri and store it to the app's SharedPreferences.
  2. when the uploadTask completes, remove the sessionUri from the app's SharedPreferences.
  3. when the app restarts, check if there is a sessionUri in the SharedPreferences. If so: resume that upload.

这篇关于Firebase - 在互联网离线时上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆