首页 > 解决方案 > 如何在一个文档中存储多个图像(Firestore)Android

问题描述

用户应该能够在一篇文章中上传多张图片。我试图将所有 url 添加到 ArrayList,然后将它们存储到 firestore。我得到的只是错误。

“imageList”总是返回 null

这是我的代码:

fun uploadPost(images:ArrayList<Uri>, imageCount: Int, description:String){

    images.forEach { image->
        val imageRef = storageReferenence.child(System.currentTimeMillis().toString()
                +"."+ image.lastPathSegment)
        val uploadTask = imageRef.putFile((image))
        uploadTask.addOnSuccessListener {
            val downloadUrl = imageRef.downloadUrl
            downloadUrl.addOnSuccessListener {uri->
                Log.d("IMAGES", uri.toString())
                imageList!!.add(uri.toString())

                count++
            }
        }

    }



    //firebaseRepository.firestoreDB.collection("post").document(firebaseRepository.userid.toString()).update(post)
}

在此处输入图像描述

您可以看到 Log 具有 url,但是当我尝试将其添加到 imageList 时,它没有这样做。

这会导致错误:imageList!!.add(uri.toString()) 错误消息:AddViewModel$uploadPost$$inlined$forEach$lambda$1$1.onSuccess

我真的不知道将图像存储为数组或像这样存储每个图像更好: image1:url... , image2: url.. 我需要它们成为同一个文档的一部分。

标签: androidimagegoogle-cloud-firestorefirebase-storage

解决方案


推荐阅读