1

I am using Firestore and Firebase storage to upload the image. But when the upload process is started and if there is no internet connection, it is still showing progress bar. I want to stop loading and it should display the message.

Thanks in advance!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Firestore and Firebase enable offline storage [by default](https://firebase.google.com/docs/firestore/manage-data/enable-offline). What you should do if you want to achieve this behavior is checking [if network connectivity is present](https://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android), and iff it is available, process to the transaction. – Arthur Attout Nov 27 '19 at 12:10
  • Thank for the solution. But there is code to check the connectivity and it is working fine. What I want is that If no internet, the progress bar should stop loading and the message should be displayed. – Shyamaly Lakhadive Nov 27 '19 at 12:26
  • Well, I think I described the way you should do it. If you have some code that checks for connectivity, and that works properly, just use it before your transaction .. There just won't be any progress bar at all, because the transaction won't even start. – Arthur Attout Nov 27 '19 at 12:58

3 Answers3

2

The Firebase SDK for Cloud Storage will automatically retry file uploads and resume in the case that the connection is lost and regained. The upload will not just immediately fail when the connection is lost.

If you want to cancel the upload if the connection is lost, you will have to detect that situation on your own and manually cancel the upload.

https://firebase.google.com/docs/storage/android/upload-files#manage_uploads

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • On iOS I am suprised to see uploads starts afresh and not continue where it was for instance if the progress was 0.43 when the network was lost. After regaining it starts again from 0.0 to 1.0 why it does not resume where it was?? – Xenolion Jul 18 '23 at 10:57
2

Thank you all for help. But what I found (from my problem) is Firebase doesn't take care of Network Connectivity. I referred this solution and my problem is solved. It also restarts the activity.

1

try this one ,when internet connection failed onFailure() method called automatically.then you hide the loading and display messages in onFailure().

 //this method will upload the file
 public void uploadFile(String mFilePath, String imageName, CallBackUploadImage callback) {

    //if there is a file to upload
    if (mFilePath != null) {
        Uri imagePath = Uri.fromFile(new File(mFilePath));
        StorageReference riversRef = mStorageRef.child("images/" + imageName + ".jpg");
        riversRef.putFile(imagePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        @SuppressWarnings("VisibleForTests")
                        Uri downloadUrl = taskSnapshot.getDownloadUrl();
                        callback.onSuccess();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        callback.onServerError();
                    }
                });
    }

}
Avinash
  • 867
  • 4
  • 11