0

When I try to upload an image onto firebase, the image url is something like "com.google.android.gms.tasks.zzw@f6699b3" instead of "https://firebasestorage.googleapis.com/...". The image itself uploads to Firebase Storage just fine, but issue right now is with the imageUrl in Realtime Database.

Below is my addToDatabase method.

private void addToDatabase(String familyName, String titleTxt, String descrptnTxt, String pointsTxt, String status) {
        if(mImageUri != null){
            StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
                    + "." + getFileExtension(mImageUri));

            mUploadTask = fileReference.putFile(mImageUri)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            String choreId = mRootRef.child("FamilyFunctions").child(familyName).child("Chores").push().getKey();
                            Chores chore = new Chores(assignedTo, descrptnTxt, choreId, mStorageRef.getDownloadUrl().toString(), pointsTxt, status, titleTxt);
//                            Chores chore = new Chores(titleTxt, descrptnTxt, pointsTxt, status, choreId, taskSnapshot.getMetadata().getReference().getDownloadUrl().toString(), assignedTo);
                            mRootRef.child("FamilyFunctions").child(familyName).child("Chores").child(choreId)
                                    .setValue(chore).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if(task.isSuccessful()){
                                                Toast.makeText(PostActivity.this, "Chore successfully created!", Toast.LENGTH_SHORT).show();
                                            }else{
                                                Toast.makeText(PostActivity.this, "Error. Couldn't add chore.", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(PostActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });

        } else {
            Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
        }

Below is my chores model:

public class Chores {

    public String assignedTo;
    public String description;
    public String id;
    public String imageUrl;
    public String pointsWorth;
    public String status;
    public String title;

    public Chores() {
    }

    public Chores(String assignedTo, String description, String id, String imageUrl, String pointsWorth, String status, String title) {
        this.assignedTo = assignedTo;
        this.description = description;
        this.id = id;
        this.imageUrl = imageUrl;
        this.pointsWorth = pointsWorth;
        this.status = status;
        this.title = title;
    }

//getters and setters etc.
//...
}

I have tried other methods of writing "mStorageRef.getDownloadUrl().toString()" and changing it to taskSnapshot.getMetadata().getReference().getDownloadUrl() but so far nothing else has worked, and I'm trying to avoid rewriting all of my code. Can someone please help me??

  • 1
    getDownloadUrl returns a Task just like putFile, not the URL. – Doug Stevenson Nov 24 '22 at 23:30
  • If you understand Kotlin, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-upload-an-image-to-cloud-storage-and-save-the-url-in-firestore-42711ca1df46) will definitely help. Here is the corresponding [repo](https://github.com/alexmamo/CloudStorageJetpackCompose). – Alex Mamo Nov 25 '22 at 09:20

0 Answers0