0

I am trying to access the actual URL of uploaded image on firebase, but not getting.

Look at my code below:

String ActualImageUrl = null;
    taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
        @Override
        public void onSuccess(Uri uri)
        {
            ActualImageUrl = uri.toString();
            Log.d("okok","url inside: "+ActualImageUrl);
        }
    });
Log.d("okok","url ouside: "+ActualImageUrl);

okok: url inside: ACTUAL URL HERE okok: url outside: null

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Your `url` will always `null` outside the callback since the `onSuccess()` method has an asynchronous behavior. So please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Jun 09 '19 at 09:23
  • @AlexMamo Please don't mark questions as a duplicate of a question that is about a different product. While the link may be useful, it can't be a duplicate. – Frank van Puffelen Jun 09 '19 at 15:03
  • @Jatinder: as Alex commented, the `onSuccess` is called *after* your `Log.d("okok","url ouside: "+ActualImageUrl);` has run, so it logs the `ActualImageUrl` before `onSuccess` has updated it. Any code that needs the download URL needs to be inside `onSuccess` or be called from there. For an example see: https://stackoverflow.com/questions/53127996/getdownloadurl-isnt-inputting-the-link-i-need/53128190#53128190, https://stackoverflow.com/a/49802185, and probably a few more from this list: https://stackoverflow.com/search?q=%5Bfirebase-storage%5D%5Bandroid%5D+download+URL+asynchronous – Frank van Puffelen Jun 09 '19 at 15:05
  • @FrankvanPuffelen Copy that, thanks puf. – Alex Mamo Jun 10 '19 at 10:46

1 Answers1

1

From Firebase Official guide use this code to get download url

final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);

Task<Uri> urlTask = uploadTask.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();
    }

    // Continue with the task to get the download URL
    return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
    if (task.isSuccessful()) {
        Uri downloadUri = task.getResult();
    } else {
        // Handle failures
        // ...
    }
}
});
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • there is no uploadTask.continueWithTask() method – Jatinder Verma Jun 11 '19 at 10:52
  • I done it. What I did is: taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Uri uri) { // create a user object by passing name as well as URL of the image User u = new User(userName, uri.toString()); } }); // finally I can get the name as well as URL from that object anywhere – Jatinder Verma Jun 19 '19 at 02:57