'How to read files data in kotlin android from internal path stored? Type mismatch: inferred type is Uri but Path! was expected [duplicate]

I want to read data by using path of the file. I have read path of the file using this code:-

 val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.setType("*/*")
            startActivityForResult(intent, 1)


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            val imagedata = data?.data
            val name = imagedata.toString()
            namedata = name.substringAfterLast("/")
            filepath = imagedata!!.path
            file_ac_name.text = filepath
        }
    }
}

This data is stored in SQLite file. I have stored the path there. The model class code is:-

data class ACMODEL (
    val id_ac: Int,
    val title_ac: String,
    val filepath_ac: String,
    val fileName_ac: String
): Serializable

here is adapter code that is use to show data in mainActivity. I am able to show all the data but I want open the file after clicking the adapter. But it is giving me this error:-

Type mismatch: inferred type is Uri but Path! was expected.

Here is my adapter code:-

open class ACDatabaseAdapter(
    private val context: Context,
    private var list: ArrayList<ACMODEL>
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var reader: BufferedReader? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHolder(
            LayoutInflater.from(context).inflate(
                R.layout.recylcerview,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val model = list[position]

        if (holder is MyViewHolder) {
            holder.itemView.tvTitle.text = model.title_ac
            holder.itemView.recyclerView_adapter.setOnClickListener {
                 reader = Files.newBufferedReader(model.filepath_ac.toUri(), StandardCharsets.UTF_8)
            }
        }
    }

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

    private class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
}


Can someone please help me?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source