0

I have data from Firebase that is displayed in a recyclerview

Data from Firebase

When a user clicks the download button. I want my app to download the specific image of the button that was clicked into the device storage.

Code responsible for showing Firebase images. This code is in a fragment class

Code relating to my Firebase storage:

@SuppressLint("NotifyDataSetChanged")
private fun GetDataFromFirebase() {
    val query: Query = myRef3!!.child("Abstract")
    query.addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            for (dataSnapshot: DataSnapshot in snapshot.children) {
                val abstract = Abstract()
                abstract.abstract = dataSnapshot.child("abstract").value.toString()
                abstractlist.add(abstract)
            }
            recyclerAdapterAbstract = abstractlist.let {
                AbstractAdapter(requireContext(),
                    it
                )
            }
            recyclerView.adapter = recyclerAdapterAbstract
            recyclerAdapterAbstract!!.notifyDataSetChanged()
        }

        override fun onCancelled(error: DatabaseError) {}
    })
    if (recyclerAdapterAbstract != null) recyclerAdapterAbstract!!.notifyDataSetChanged()
}

private fun ClearAll() {
    abstractlist.clear()
    abstractlist = ArrayList()
}

Adapter class:

class AbstractAdapter(private val mContext: Context, private val abstractList: List<Abstract>) :
    RecyclerView.Adapter<AbstractAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.abstract_image_view, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        Glide.with(mContext)
            .load(abstractList[position].abstract)
            .into(holder.imageView)
    }

    override fun getItemCount(): Int {
        return abstractList.size
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.abstractImageView)

    }

    companion object {
        private const val Tag = "RecyclerView"
    }
}

I want to know how to download these images individually from Firebase storage.

Khumo Mashapa
  • 390
  • 1
  • 4
  • 13
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Jun 04 '22 at 07:34
  • There's no error in my code. I just want to know how to download these images one at a time from Firebase, but I don't know how to go about it. – Khumo Mashapa Jun 04 '22 at 11:15
  • Do you have the correct URL? – Alex Mamo Jun 04 '22 at 11:34
  • 1
    The use of keyword `abstract` as a variable name is questionable. And usually, when knowing the name of the bucket & the object, one can run a `FileDownloadTask` in order to fetch it. For reference: https://firebase.google.com/docs/storage/android/download-files#kotlin+ktx – Martin Zeitler Jun 04 '22 at 12:16

0 Answers0