'Firebase getDownloadURL returns null - android

I'm using Firebase storage and firebase firestore and I want to add uploaded image's url to firestore document's PostImageURL field. But when I tried it gets null value not it's download url.

private FirebaseFirestore firebaseFirestore;
private FirebaseStorage firebaseStorage;
private Map<String,Object> postInfo;
private StorageReference postImageReference,storageReference;
private String postImageURL;

It's my upload method to firebase storage

private void uploadPostImageToFirestorage(String postID,Bitmap postImageBitmap)
{
    storageReference = firebaseStorage.getReference();
    postImageReference = storageReference.child("post_images/"+postID+".jpg");
    byte[] postImageInBytes = compressImageAndConvertByteArray(postImageBitmap);
    UploadTask uploadTask = postImageReference.putBytes(postImageInBytes);
    uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(PostUploadActivity.this, "Post image uploaded!", Toast.LENGTH_SHORT).show();
            postImageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    // get postImageReference path's download url
                    // postImageURL global scope variable
                    postImageURL = uri.toString();
                }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(PostUploadActivity.this, "Couldn't get image url. Error: " + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(PostUploadActivity.this, "Post image upload failure! Error: " + e.toString(), Toast.LENGTH_SHORT).show();
                }
            });
}

And it's my method that adds post info to firestore and post image's download url

private void sharePost()
    {
        UUID postID = UUID.randomUUID();
        postTitle = binding.postTitleET.getText().toString();
        postComment = binding.postCommentET.getText().toString();
        postOwnerID = currentUser.getUid();
        postInfo = new HashMap<>();
        if(!checkInputsAreEmpty(postTitle,postComment)){
            postInfo.put("PostOwnerID",postOwnerID);
            postInfo.put("PostID",postID);
            postInfo.put("PostTitle",postTitle);
            postInfo.put("PostComment",postComment);
            if(locationActivated){
                postInfo.put("PostLocation",fullAddress);
            }
            if(postImageSelected){
                uploadPostImageToFirestorage(postID.toString(),postImageBitmap);
                postInfo.put("PostImageURL",postImageURL);
            }
            firebaseFirestore
                    .collection("post_info")
                    .document(postID.toString())
                    .set(postInfo)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void unused) {
                            Toast.makeText(PostUploadActivity.this, "Post info successfully added into firestore!", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(PostUploadActivity.this, "Error : " + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }
        else{
            Toast.makeText(this, "Please fill post title and post comment!", Toast.LENGTH_SHORT).show();
        }
    }

And it looks like on firestore



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source