0

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?
Prasad v Bhagat
  • 436
  • 2
  • 11
  • "I have read path of the file using this code" -- *if* the `Uri` you get back has `file` as its scheme, *then* `getPath()` will return a filesystem path. Most likely, you will not get a `Uri` that has `file` as its scheme, at which point your entire approach falls apart. "But it is giving me this error" -- you are calling `toUri()` on a random `String`, then passing that as a value for a parameter that needs to be a `Path`, not a `Uri` or a `String`. – CommonsWare Mar 25 '22 at 11:27
  • Then how should I get the path? So, that the path will open in adapter. – Prasad v Bhagat Mar 25 '22 at 11:54
  • "Then how should I get the path?" -- there is no path. `ACTION_GET_CONTENT` is not limited to dealing with files on the filesystem, let alone files that you can access. – CommonsWare Mar 25 '22 at 11:59
  • How to get access to the files. – Prasad v Bhagat Mar 25 '22 at 12:04
  • What should I do to get desired results? – Prasad v Bhagat Mar 25 '22 at 12:06
  • I do not know what you mean by "get access to the files" or what "desired results" refers to. If you are trying to read in the content identified by that `Uri`, use `ContentResolver` and `openInputStream()`. Do bear in mind, though, that you will need to get rid of your database, as your rights to access that content do not last beyond the scope of your running process. If you want *long term* access to user-selected content, use `ACTION_OPEN_DOCUMENT`, and use `takePersistableUriPermission()`: https://commonsware.com/blog/2020/08/08/uri-access-lifetime-still-shorter-than-you-might-think.html – CommonsWare Mar 25 '22 at 12:08

0 Answers0