hello i have a problem importing a TXT file to a Text view in Android Kotlin

Viewed 190

hello I have a problem importing a TXT file to a Text view in Android Kotlin

I have gotten the permissions for the address of the file but I don't know how to read it and put it in the text view

i make a video to show you https://youtu.be/w2YaicpQUYM

the file I choose is in Download / sample.txt

reslutado cotent://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fsample.txt

lateinit var filepath : Uri        
button1.setOnClickListener {
            permisos()
        }
    

private fun permisos() {
    if(ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
        Toast.makeText(this, "permisos concedidos",Toast.LENGTH_SHORT).show()
        txtchoser()
    }else{
        ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),1000)
        Toast.makeText(this, "sin permisos",Toast.LENGTH_SHORT).show()
    }
}

private fun txtchoser() {
    var intent = Intent()
    intent.setType("text/*")
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    startActivityForResult(Intent.createChooser(intent,"selecciona el archivo"),1000)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode==1000 && resultCode == Activity.RESULT_OK && data !=null){
        filepath =data.data!!
        text1.setText("$filepath")
        Toast.makeText(this, "Datos subidos correctamente",Toast.LENGTH_SHORT).show()
    }
}
0 Answers
Related