1

I have data/images from Firebase Realtime Database that is displayed in a recyclerview. In that recyclerview I placed a button that will allow users to download whatever image they choose. I ran the code and pressed the button, but nothing happened.

Code

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) {


    holder.download_btn.setOnClickListener {
        downloadFile()

    }

    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)
    val download_btn: Button = itemView.findViewById(R.id.abstractDownloadBtn)


}

private fun downloadFile() {
    val storage = FirebaseStorage.getInstance()
    val storageRef = storage.getReferenceFromUrl("https://notes-72413.firebaseio.com/")
    val islandRef = storageRef.child("abstract")
    val rootPath = File(Environment.getExternalStorageDirectory(), "file_name")
    if (!rootPath.exists()) {
        rootPath.mkdirs()
    }
    val localFile = File(rootPath, "imageName.txt")
    islandRef.getFile(localFile)
        .addOnSuccessListener(OnSuccessListener<FileDownloadTask.TaskSnapshot?> {
            Log.e("firebase ", ";local tem file created  created " + localFile.toString())
            //  updateDb(timestamp,localFile.toString(),position);
        }).addOnFailureListener(OnFailureListener { exception ->
            Log.e(
                "firebase ",
                ";local tem file not created  created $exception"
            )
        })
}

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

My logcat error

 Firebase Storage URLs must point to an object in your Storage Bucket. Please obtain a URL using the Firebase Console or getDownloadUrl()

Here's a screenshot for a reference of what my layout looks like

0 Answers0