0

I am making an android app in java and I need to upload a file on firebase storage. The problem is that the methods onFailure and onSuccess are void and can't return anything ! I need to know when the upload is finished if it's a success or not...

I try too to modify a variable in the class or in the method but it dosn't works because the listener are asynchronous ... Here is my java class :

public class MyFirebase {

// Upload picture to firebase 
public Boolean uploadToFirebase (Uri path, String nameFile, FirebaseStorage storage) throws InterruptedException {

    // Create a storage reference from the app
    StorageReference storageRef = storage.getReference();
    UploadTask uploadTask;

    // START upload
    Uri file = Uri.fromFile(new File(String.valueOf(path)));
    final StorageReference ref = storageRef.child("scanned_images/"+nameFile);
    uploadTask = ref.putFile(file);


    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.d("upload", "uploadToFirebase: fail ");
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Log.d("upload", "uploadToFirebase: success ");
        }
    });


     return uploadTask.isSuccessful();

}

}

  • Firebase APIs are asynchronous, you cannot simply return such a result synchronous. You always should wait for the result. **[Here is an example](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)**. – Alex Mamo Apr 15 '21 at 15:34

2 Answers2

1

You need to create listener for this because these calls happen asynchronously you can't return it like that.

You can create a interface like below.

public interface MyResponseListener {

    void onSuccess();

    void onFailure();
}

And pass it as a argument in your method.

// Upload picture to firebase

public void uploadToFirebase (Uri path, String nameFile, FirebaseStorage storage,
final MyResponseListener myResponseListener) throws InterruptedException {

    // Create a storage reference from the app
    StorageReference storageRef = storage.getReference();
    UploadTask uploadTask;

    // START upload
    Uri file = Uri.fromFile(new File(String.valueOf(path)));
    final StorageReference ref = storageRef.child("scanned_images/"+nameFile);
    uploadTask = ref.putFile(file);


    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.d("upload", "uploadToFirebase: fail ");
            myResponseListener.onFailure();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Log.d("upload", "uploadToFirebase: success ");
             myResponseListener.onSuccess();
        }
    });




}

And finally use this where you like:

private void setListener() {
        myFirebase.uploadToFirebase(path, nameFile, storage,new MyResponseListener() {
            @Override
            public void onSuccess() {
                //handle success
            }

            @Override
            public void onFailure() {
               //hanlde failure
            }
        });
    } 
Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41
0

I think it's not possible, but maybe you can return the result of your function inside onSuccess and onFailure functions

uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.d("upload", "uploadToFirebase: fail ");
            return false
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Log.d("upload", "uploadToFirebase: success ");
            return true
        }
    });
luchin
  • 52
  • 5