How to Use Getdownloadurl in Recent Versions

How to use getdownloadurl in recent versions?

The taskSnapshot.getDownloadUrl() method was removed in recent versions of the Firebase Storage SDK. You will need to instead get the download URL from the StorageReference now.

Calling StorageReference.getDownloadUrl() returns a Task, since it needs to retrieve the download URL from the server. So you'll need a completion listener to get the actual URL.

From the documentation on downloading a file:

 storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png' in uri
System.out.println(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});

Alternatively that first line could be this if you're getting the download URL right after uploading (as in your case):

taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

Also see:

  • Firebase Storage getDownloadUrl() method can't be resolved
  • Error: cannot find symbol method getDownloadUrl() of type com.google.firebase.storage.UploadTask.TaskSnapshot
  • The example in the Firebase documentation on uploading a file and getting its download URL

Firebase Storage getDownloadUrl() method can't be resolved

The Firebase API has changed.

May 23, 2018

Cloud Storage version 16.0.1

Removed the deprecated StorageMetadata.getDownloadUrl() and UploadTask.TaskSnapshot.getDownloadUrl() methods. To get a current download URL, use StorageReference.getDownloadUr().

UploadTask.TaskSnapshot has a method named getMetadata() which returns a StorageMetadata object.

This StorageMetadata object contains a method named getReference() which returns a StorageReference object.

That StorageReference object contains the getDownloadUrl() method, which now returns a Task object instead of an Uri object.

This Task must then be listened upon in order to obtain the Uri, which can be done asynchronously or in a blocking manner; see the Tasks API for that.

Android Studio can't get Storage download URL

Could you try this:

fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Download URL
String url = uri.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Errors
}
});


Related Topics



Leave a reply



Submit