How to use getdownloadurl in recent versions?

后端 未结 4 647
庸人自扰
庸人自扰 2020-11-22 00:29
                Uri downloaduri=taskSnapshot.getDownloadUrl();//here i cant use getdownloadurl() function
                DatabaseReference new_prod=db.push();
              


        
4条回答
  •  故里飘歌
    2020-11-22 00:52

    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() {
        @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() {
    

    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

提交回复
热议问题