Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - uploading images when internet is offline

Tags:

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 automatically the data to the db.

is it also possible to do it with the Firebase storage like send images even if the internet is off, and then when the internet is on again, it will send the images files automatically?

If so, how can I do it? If not with Firebase, any other option?

like image 281
dani jinji Avatar asked Aug 08 '16 06:08

dani jinji


2 Answers

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.
like image 96
Frank van Puffelen Avatar answered Sep 29 '22 13:09

Frank van Puffelen


Technically, the accepted answer is incorrect given the OP question, which is,

send images even if the internet is off

The accepted answer talks about resuming a download once started, which correct in detail but does not address the answer correctly.

The corrected answer is "no", you cannot upload an image to Firebase Firestore if the device is not connected to the internet, the upload will fail and there is no auto-restart of the upload operation.

As previously noted, you must be connected to the internet, at lease long enough to start the upload and get an uploadsessionuri URI from Firebase. Once the upload has started then you can resume the upload using this sample code or the code above.

As noted in the documentation, the resume URI is valid for about 7 days but it is the developer's responsibility to ensure that the file contents has not changed since the start of the upload.

like image 31
Kenneth Argo Avatar answered Sep 29 '22 13:09

Kenneth Argo