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?