0

Hi i want to get downloadUrl but i got error I'm tried some methods like this

var firebaseURL =p0?.uploadSessionUri.toString() the result is url but if i open this url, i get this error Invalid request. X-Goog-Upload-Command header is missing. ( i'm new in kotlin)

uploadTo.addOnSuccessListener (object :OnSuccessListener<UploadTask.TaskSnapshot>{

        override fun onSuccess(p0: UploadTask.TaskSnapshot?) {

            var firebaseURL =p0.downloadUrl // i got error in this line

            FirebaseDatabase.getInstance().reference
                .child("Users")
                .child(FirebaseAuth.getInstance().currentUser?.uid!!)
                .child("imagurl")
                .setValue(firebaseURL.toString())

        }
    })
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Jim
  • 87
  • 1
  • 7

1 Answers1

0

The downloadUrl "property" here is not actually a property at all. It's a method called getDownloadUrl that returns another Task that asynchronously fetches the URL. It doesn't return a URL directly. Refer to the documentation on how to use it correctly. For example:

val ref = storageRef.child("images/mountains.jpg")
uploadTask = ref.putFile(file)

val urlTask = uploadTask.continueWithTask { task ->
    if (!task.isSuccessful) {
        task.exception?.let {
            throw it
        }
    }
    ref.downloadUrl
}.addOnCompleteListener { task ->
    if (task.isSuccessful) {
        val downloadUri = task.result
    } else {
        // Handle failures
        // ...
    }
}

What's happening here is:

  1. The file is being uploaded with uploadTask.
  2. A continuation is added to that Task as a callback when it's complete
  3. ref.downloadUrl returns another Task to get the URL.
  4. Another callback is added to the chain to handle the fetched URL.

See also: How to get URL from Firebase Storage getDownloadURL

The fact of the matter is that this API was not very well designed. The method starts with "get" but is not really a property getter.

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