0

i have checked available resources and yet either i get error or at best i get com.google.android.gms.tasks.zzu@5f9a842 as the uri

Following this latest google doc here, i still got the com.gms as url

here is my code

            val fileRef = storageProfilePicRef!!.child(firebaseUser.uid + "jpg")

            val uploadTask = fileRef.putFile(imageUri!!)
            uploadTask.continueWith {
                if (!it.isSuccessful) {
                    pd.dismiss()
                    it.exception?.let { t ->
                        throw t
                    }
                }
                fileRef.downloadUrl
            }.addOnCompleteListener {
                if (it.isSuccessful) {
                    val downloadUrl = it.result
                    myUri = downloadUrl.toString()

                    print("Task: ${downloadUrl}")
                    ...

Terminal: I/System.out: Task: com.google.android.gms.tasks.zzu@4298dc1(HTTPLog)-Static: isSBSettingEnabled false

General Grievance
  • 4,259
  • 21
  • 28
  • 43
X-Black...
  • 1,158
  • 1
  • 16
  • 28
  • 1
    The duplicate primarily talks about java, but the underlying issue is the same. downloadUrl returns a Task, not a URL. You have to deal with it asynchronously just like the Task returned from putFile. See also the documentation: https://firebase.google.com/docs/storage/android/download-files#download_data_via_url – Doug Stevenson Apr 05 '20 at 23:23
  • what's the difference between OnCompleteListener and OnSuccesListener ? – X-Black... Apr 05 '20 at 23:28
  • Tried it, still didn't work...pls what is the best way to get the uri after uploading it...? – X-Black... Apr 05 '20 at 23:45
  • 1
    The way shown in the documentation. If you have made a new attempt using what you know already about Tasks (you already used one with putFile), please post a new question showing what you have that doesn't work the way you expect. – Doug Stevenson Apr 05 '20 at 23:57
  • my code is exactly what is shown here in the firebase doc on how to get download url after uploading – X-Black... Apr 06 '20 at 00:09
  • Did you read the duplicate that I marked to understand what you did wrong? Did you click through to the documentation link that I provided in the first comment? If you did either of those things, you would better understand how to proceed. – Doug Stevenson Apr 06 '20 at 00:22
  • @DougStevenson As far as I can see OP **is** calling `getDownloadURL()`, which translates to `downloadUrl` in Kotlin. In fact their code is pretty similar to https://firebase.google.com/docs/storage/android/upload-files#get_a_download_url. I'm not sure why it's printing a serialized `Task`. – Frank van Puffelen Apr 06 '20 at 02:06
  • @X-Black... The main difference I see between your code and that in the [documentation](https://firebase.google.com/docs/storage/android/upload-files#get_a_download_url) is that you use `it`, while the documentation explicitly declares the `task`. I'd recommend using explicit `task` declarations, as shown in the docs. – Frank van Puffelen Apr 06 '20 at 02:09
  • 2
    @FrankvanPuffelen My mistake. They're just logging the wrong thing. – Doug Stevenson Apr 06 '20 at 02:45

2 Answers2

0

You're logging the wrong value. Instead of this:

print("Task: ${downloadUrl}")

Log this:

print("Task: ${downloadUrl.result.toString()}")

Your downloadUrl variable is not correctly named. It's a Task, not a URL. It might be clearer like this:

val task = it.result
val uri = task.result
val uriAsString = uri.toString()
Doug Stevenson
  • 268,359
  • 30
  • 341
  • 380
0

Solved !!!

        val fileRef = storageProfilePicRef!!.child(firebaseUser.uid + "jpg")

        val uploadTask = fileRef.putFile(imageUri!!)
        uploadTask.continueWith {
            if (!it.isSuccessful) {
                pd.dismiss()
                it.exception?.let { t ->
                    throw t
                }
            }
            fileRef.downloadUrl
        }.addOnCompleteListener {
            if (it.isSuccessful) {
                val downloadUrl = it.result //Here returns a task..
                myUri = downloadUrl.toString()

                print("Task: ${downloadUrl}")
                ...

val downloadurl = it.result returns a Task

correct it by adding an addOnSuccessListener

so the correct code is

val uploadTask = fileRef.putFile(imageUri!!)
        uploadTask.continueWith {
            if (!it.isSuccessful) {
                pd.dismiss()
                it.exception?.let { t ->
                    throw t
                }
            }
            fileRef.downloadUrl
        }.addOnCompleteListener {
            if (it.isSuccessful) {
                it.result!!.addOnSuccessListener{task ->
                                 myUri = task.toString()
                                print("$myUri")
                                 ...
                                    }
X-Black...
  • 1,158
  • 1
  • 16
  • 28