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