0

I am trying to upload images to Real time Firebase Database by creating two folders using Compressor library and need to display image like messenger with username but i am unable to display image due to url issue

            var filePath = mStorageRef!!.child("chat_profile_images")
                .child(userId + ".jpg")

            //Create another directory for thumbimages ( smaller, compressed images)
            var thumbFilePath = mStorageRef!!.child("chat_profile_images")
                .child("thumbs")
                .child(userId + ".jpg")


            filePath.putFile(resultUri)
                .addOnCompleteListener{
                        task: Task<UploadTask.TaskSnapshot> ->
                    if (task.isSuccessful) {

                        //Let's get the pic url
                        var donwloadUrl = task.result?.storage?.downloadUrl.toString()

                        Log.d(TAG, "Profilepic link: $donwloadUrl")

                        //Upload Task
                        var uploadTask: UploadTask = thumbFilePath
                            .putBytes(thumbByteArray)

                        uploadTask.addOnCompleteListener{
                                task: Task<UploadTask.TaskSnapshot> ->
                            var thumbUrl = task.getResult()?.storage?.downloadUrl.toString()

                            Log.d(TAG, "Profilepic link: $thumbUrl")

i tried to change downloadUrl

filepath.downloadUrl.toString
thumbFilePath.downloadUrl.toString

but both these values getting "com.google.android.gms.tasks.zzu"

i also tried to change

task.result.sessionurl.downloadUrl.toString

for this one i am getting downloadUrl but not a complete solution for my problem as still i cannot display image i need to get thumbUrl downloadUrl

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Srikanth G
  • 80
  • 9

3 Answers3

0

You have the exact same and very common misunderstanding as in this question, except it's in java. You should follow the documentation here to understand get getDownloadUrl works. As you can see from the linked API documentation, it's not a property getter, it's actually a method that returns a Task<Uri> that tracks the asynchronous fetch of the URL you want, just like the upload task:

filePath.downloadUrl
.addOnSuccessListener { urlTask ->
    // download URL is available here
    val url = urlTask.result.toString()
}.addOnFailureListener { e ->
    // Handle any errors
}

This will only work after the upload is fully complete.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Correct way of getting download link after uploading

here

X-Black...
  • 1,376
  • 2
  • 20
  • 28
0

Just putting out there, I also encounter the same problem,

but both these values getting "com.google.android.gms.tasks.zzu"

but it wasn't the same mistake from the OP

used addOnCompleteListener instead of addOnSuccesslistener

My Error code:

imageRef.downloadUrl.addOnCompleteListener { url ->
                val imageURL = url.toString()
                println("imageURL: $imageURL , url: $url")
                addUserToDatabase(imageURL)
            }