Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

taskSnapshot.getDownloadUrl() method not working

private void uploadImageToFirebaseStorage() {
    StorageReference profileImageRef =
        FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");

    if (uriProfileImage != null) {
        progressBar.setVisibility(View.VISIBLE);
        profileImageRef.putFile(uriProfileImage)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(@NonNull UploadTask.TaskSnapshot taskSnapshot) {
                    progressBar.setVisibility(View.GONE);
                    profileImageUrl = taskSnapshot.**getDownloadUrl**().toString();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(ProfileActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    }
}

taskSnapshot.getDownloadUrl() method not working comes up with red line under it

like image 553
David G Avatar asked May 29 '18 13:05

David G


5 Answers

Edit: see this comment on why the approach in this answer doesn't work:

firebaser here This answer is wrong. While it at first may appear to work (since it compiles) the result of getDownloadUrl().toString() is not a download URL, but a string representation of a Task object. For a better answer, see stackoverflow.com/a/55503926 or the sample in the Firebase documentation.

Original answer below...


In Firebase Storage API version 16.0.1, the getDownloadUrl() method using taskSnapshot object has changed. now you can use,

taskSnapshot.getMetadata().getReference().getDownloadUrl().toString()

to get download url from the firebase storage.

like image 129
Sumit Monapara Avatar answered Oct 13 '22 03:10

Sumit Monapara


To get imageUrl path from storage use this code:

uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        if (taskSnapshot.getMetadata() != null) {
            if (taskSnapshot.getMetadata().getReference() != null) {
                Task<Uri> result = taskSnapshot.getStorage().getDownloadUrl();
                result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        String imageUrl = uri.toString();
                        //createNewPost(imageUrl);
                    }
                });
            }
        }
    }});

that is all 😉

NOTE: Do not forget initialize StorageReference and UploadTask in your uploadFile method.

like image 31
Wilmer Villca Avatar answered Oct 13 '22 05:10

Wilmer Villca


Try Using this it will download the image from FireBase storage

FireBase Libraries versions 16.0.1

Task<Uri> result = taskSnapshot.getMetadata().getReference().getDownloadUrl();
result.addOnSuccessListener(new OnSuccessListener<Uri>() {
      @Override
      public void onSuccess(Uri uri) {
             String photoStringLink = uri.toString();
      }
});
like image 32
AmrDeveloper Avatar answered Oct 13 '22 04:10

AmrDeveloper


I faced the similar error I solved it with this method. Hope it helps

uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
                task.addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        String photoLink = uri.toString();
                        Map userInfo = new HashMap();
                        userInfo.put("profileImageUrl", photoLink);
                        mUserDatabase.updateChildren(userInfo);
                    }
                });
                finish();
                return;
            }
        });
like image 21
Shivam Maindola Avatar answered Oct 13 '22 03:10

Shivam Maindola


 ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                        taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                            @Override
                            public void onComplete(@NonNull Task<Uri> task) {
                                changeProfilePic(String.valueOf(task.getResult()));//gives image or file string url
                            }
                        });

try this code will work for sure

like image 4
Zeeshan Mehdi Avatar answered Oct 13 '22 03:10

Zeeshan Mehdi