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,555
  • 31
  • 31
  • 45
X-Black...
  • 1,376
  • 2
  • 20
  • 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

3 Answers3

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
  • 297,357
  • 32
  • 422
  • 441
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,376
  • 2
  • 20
  • 28
0

i sloved the problem you should make addOnSuccessListener return uri->unit not task

}.addOnCompleteListener {
        if (it.isSuccessful) {
            it.result!!.addOnSuccessListener{uri->unit ->
                             myUri = task.toString()
                            print("$myUri")
                             ...
                                }
MR SHADOWS
  • 71
  • 1
  • 3
  • then myuri =uri.toString – MR SHADOWS Jul 13 '22 at 19:51
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 14 '22 at 17:56