[downloadUrl在图像上传到Firebase存储之前被调用

问题描述 投票:0回答:1

我正在尝试获取上传到Firebase存储的图像的downloadUrl。这是代码:

val ref = FirebaseStorage.getInstance().getReference("images/$fileName")


        ref.putFile(selectedPhotoUri!!)
            .addOnSuccessListener {
                Log.d("RegisterActivity","Successfully uploaded the image: ${it.metadata?.path}")
            }
            .addOnFailureListener {
                Log.d("RegisterActivity", "Failed to upload image: ${it.message}")
            }

        ref.downloadUrl.addOnSuccessListener {
            Log.d("RegisterActivity","File location: $it")
            saveUserToFirebaseDatabase(it.toString())
        }
        ref.downloadUrl.addOnFailureListener {
            Log.d("RegisterActivity","Location not found:${it.message}")
        }

当我运行该应用程序时,由于某种原因,当图像上传完成时会调用downloadUrl(已弃用的getDownloadUrl())。这是调试日志:

2020-04-10 07:15:44.849 12642-12642/com.example.chatapplication D/RegisterActivity: Location not found:Object does not exist at location.
2020-04-10 07:15:45.348 12642-12642/com.example.chatapplication D/RegisterActivity: Successfully uploaded the image: images/494ff560-4448-477b-bd6e-d71640eb6933

如上所述,成功上传图像在调用downloadUri之后被记录。如何确保在上传图片后调用downloadUrl?

android firebase kotlin firebase-storage
1个回答
0
投票

您应该这样做:

ref.putFile(selectedPhotoUri!!)
        .addOnSuccessListener {
             Log.d("RegisterActivity","Successfully uploaded the image: ${it.metadata?.path}")
           ref.downloadUrl.addOnSuccessListener {
             Log.d("RegisterActivity","File location: $it")
             saveUserToFirebaseDatabase(it.toString())
           }
        }
        .addOnFailureListener {
             Log.d("RegisterActivity", "Failed to upload image: ${it.message}")
        }

成功上传后调用downloadUrl方法。

© www.soinside.com 2019 - 2024. All rights reserved.