Search code examples
androidkotlingoogle-cloud-firestoreimageviewfirebase-storage

why is my image not showing in recyclerview?


I followed a tutorial on youtube on how to retrieve images and text from Firestore and display them in the RecyclerView. I'm using the Glide library to display captured images. I bind my image using this code:

override fun onBindViewHolder(holder: itemViewHolder, position: Int) {
    val produk=listOfItem[position]
    holder.kategori.text=produk.productCategory
    holder.berat.text=produk.productWeight.toString()

    //img
    Glide.with(context)
        .load(produk.productIcon)
        .into(holder.gambarBarang)

}

I try to use FirebaseStorage references:

val storage = FirebaseStorage.getInstance()

    val gsReference = storage.getReferenceFromUrl(produk.productIcon.toString())

    Glide.with(context)
        .load(produk.productIcon)
        .into(holder.gambarBarang)

but that seems to make the situation even worse, i get this error:

 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.b1, PID: 8300
 java.lang.IllegalArgumentException: The storage Uri could not be 
 parsed.

I also noticed that some of the images that I put in the imageView are not visible. Is this a result of the inserted image being too large? Anyone know how to solve it?

Edit: I try to log the produk.productIcon.toString() inside the onBindViewHolder, and get this:

D/TAG: com.google.firebase.storage.UploadTask$TaskSnapshot@7e89f3c
    gggg
D/TAG: com.google.firebase.storage.UploadTask$TaskSnapshot@213e98e
    gggg
D/TAG: com.google.firebase.storage.UploadTask$TaskSnapshot@df9e78f

I think the UploadTask come from the code that i use for adding the image to Firebase Storage:

val filePathAndName="product_images/"+""+timeStamp
val storageReference=FirebaseStorage.getInstance().
getReference(filePathAndName)

        val uploadTask= storageReference.putFile(image_Uri!!)
            .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val downloadUri = task.result
                val item = Item(
                    ""+firebaseAuth.uid,
                    timeStamp,
                    kategori,
                    berat_sampah.toDouble(),
                    deskripsi,
                    ""+downloadUri,
                )
                FirestoreClass().addItem(this@AddProductActivity, FirestoreClass().getCurrentUserID(),item)
                ClearData()

Is this code the cause of the error?


Solution

  • So based on @Abdullah Z Khan answer, i notice that the problem is with my image uri. I use task.getResult to get the uri, but it give me taskSnapShot instead.