-1

I'm making an Android application and i'm trying to get the DownloadURL of an image stored in my Firebase Storage, here's my function:

FUNCTION:

if(imageUri != null){
    StorageReference fileReference = storageReference.child(System.currentTimeMillis()+"."+getFileExtension(imageUri));
    fileReference.putFile(imageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>(){
        @Override
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception{
            if(!task.isSuccessful()){
                throw task.getException();
            }
            return storageReference.getDownloadUrl();
         }
    }).addOnCompleteListener(new OnCompleteListener<Uri>(){
        @Override
        public void onComplete(@NonNull Task<Uri> task){
            if(task.isSuccessful()){
                Uri downloadUri = task.getResult();
                u.setImageURL(downloadUri.toString());
                Toast.makeText(TelaRegistro.this, u.getImageURL(), Toast.LENGTH_LONG).show();
            }else{
                u.setImageURL("Sem imagem!");
            }
        }
    });
}else{
    u.setImageURL("Sem imagem!");
}

PROBLEM:

The problem is that when I call the method on the line below, it comes with value.

Uri downloadUri = task.getResult();
u.setImageURL(downloadUri.toString());
Toast.makeText(TelaRegistro.this, u.getImageURL(), Toast.LENGTH_LONG).show();

But when I try to call Get again it returns with null value, as is the case with the line below.

Toast.makeText(TelaRegistro.this, u.getImageURL(), Toast.LENGTH_LONG).show();

I'm a beginner programmer and I've been trying to solve this for a few days, I do not know what else to do. Could you help me?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • can you also share `Toast` class code? – Zain Ul Abideen Dec 18 '18 at 03:44
  • 1
    Toast is in-built standard android class, for displaying messages to user, similar to `snackBar`. – PradyumanDixit Dec 18 '18 at 03:46
  • So you're saying that when you call `u.getImageURL()` for the second time, it returns `null`? Is that second call also in the code you shared? Also: what is `u`? How and where did you declare that? – Frank van Puffelen Dec 18 '18 at 03:48
  • Yes; Yes, it's inside the same Java Class, but outside of the onComplete void; U it's the class who have the User information, it's declarated over the code I've shared: final Usuario u = new Usuario();. – Pedro Silveira Dec 18 '18 at 04:09
  • You can also check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Dec 18 '18 at 09:42

1 Answers1

0

I didn't fully get what you mean by private class and getting value outside it, in relation to downloadURl() which is not the class' attribute.

But what I understand from your question is that you want to retrieve the value of your image link, somewhere else than the place you're getting it now.

You can duplicate this code at that place again, as Firebase calls are asynchronous, so outside the eventListener you may get null values for the variables in which you store value retrieved from Firebase.

Read more about asynchronous behaviour of Firebase here.

Instance 1

Instance 2

PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20