taskSnapshot.getDownloadUrl() is deprecated

Liliana J picture Liliana J · May 22, 2018 · Viewed 20.2k times · Source

Until now, the way to get the url from file on Storage in Firebase, I used to do this taskSnapshot.getDownloadUrl, but nowadays is deprecated, which method I should use?

enter image description here

Answer

Gastón Saillén picture Gastón Saillén · May 22, 2018

As Doug says, you will need to run it inside a Task

Here is a hint of how you need to implement it

final StorageReference ref = storageRef.child("your_REF");
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();
                String downloadURL = downloadUri.toString();
            } else {
                // Handle failures
                // ...
            }
        }
    });

For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41