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();
}
});
}
}