I'm trying to read the csv file with Cotlin, but the letters are broken.

Asked 1 years ago, Updated 1 years ago, 110 views

You want to read the csv file stored in the external storage via Cotlin. I succeeded in the text file, but the characters in the csv file are broken like a gram, so there is no change even if I change the Android studio encoding setting to utf-8. What should I do?

https://drive.google.com/file/d/1cGt6GZxj3ceykQIj5J-tw79trBP8AlNy/view?usp=sharing https://drive.google.com/file/d/1BZk1tePw5k_y9Oo3wNx_mMB9hDRCURlr/view?usp=sharing (The picture was not uploaded, so I inevitably upload it as a Google drive image)

Cotton file

class GetTxtActivity : AppCompatActivity() {

    private val vBinding by lazy {ActivityGetTxtBinding.inflate(layoutInflater)}
    val getCsvCode:Int = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(vBinding.root)

        vBinding.getCellBtn.setOnClickListener{
            val downUri = Uri.parse("")
            openCsvFile(downUri)
        }
    }

    private fun openCsvFile(pickerInitialUri: Uri){
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
            addCategory(Intent.CATEGORY_OPENABLE)
            type = "text/csv/dnf/*"
            putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
        }
        startActivityForResult(intent, getCsvCode)
    } //openCsvFile End

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, requestCode, data)
        if(requestCode == getCsvCode && resultCode == Activity.RESULT_OK){
            data?.data?.also { uri ->
                val inp = getContentResolver().openInputStream(uri)
                inp?.bufferedReader()?.useLines { lines->
                    for(item in lines){
                        Log.d("test", item)
                    }
                }

            }
        }
    } //onActivityResult End

} // GetTxt End

kotlin android-studio

2022-09-30 01:00

1 Answers


2022-09-30 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.