-1

I'm quite new to Kotlin and Android programming.

I tried to find solution for my problem everywhere and couldn't do it. but it seems so trivial.

How do I read a file formatted as .csv from the external storage to a map variable?

I would love the whole code in order to do it (for premission, reading etc), lets name the file myFile.csv and locate it on Download folder (or any othe folder if it is a problem)

I'm using android 11 (need to stay compatible in the future)

EDIT: I tried all the method listed in the answers that were in the other question that was connected to my question. I still always get: open failed: EACCES (Permission denied)

My First Try for MainActivity:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setupPermissions()

    val path = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/LET/test.txt")

    val resolver = applicationContext.contentResolver
    val readOnlyMode = "r"
    val parcelFile = resolver.openFileDescriptor(path.toUri(), readOnlyMode)
    val fileReader = FileReader(parcelFile!!.fileDescriptor)
    val reader = BufferedReader(fileReader)
    var line: String?

    while (reader.readLine().also { line = it } != null) {
        Toast.makeText(this, line, Toast.LENGTH_LONG).show()
    }
    reader.close()
    reader.close()
    fileReader.close()

}


private fun setupPermissions() {
    val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
    if (permission == PackageManager.PERMISSION_GRANTED) {
        val path = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
    } else {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
            101
        )
    }

  }
}

My Second Try for MainActivity:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setupPermissions()

    val path = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
    Toast.makeText(this, path.toString(), Toast.LENGTH_LONG).show()

    val letDirectory = File(path, "LET")
    letDirectory.mkdirs()
    val file = File(letDirectory, "test.txt")

    val inputAsString = FileInputStream(file).bufferedReader().use { it.readText() }
}

private fun setupPermissions() {
    val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
    if (permission == PackageManager.PERMISSION_GRANTED) {
        val path = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
    } else {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
            101
        )
    }

}

Thanks, Tal.

Martin Zeitler
  • 59,798
  • 15
  • 122
  • 186
  • Please notice the "Search..." box on top of the page; eg. https://stackoverflow.com/questions/tagged/kotlin. – Martin Zeitler May 25 '22 at 19:47
  • 1
    It was 4 years ago an I already tried it. Maybe something changed since then – Tal Yehuda May 25 '22 at 21:37
  • That's not a valid argument. Likely I could add a whole bunch of duplicates (please search for yourself). `EACCES` means you're not having granted permission to do so. Your multiple attempts do not make it easier but more clutter; please stick with one version... one does not simply "setup" a permission, but has to handle the callback... which means you're accessing it too soon. It's indeed too trivial. – Martin Zeitler May 26 '22 at 07:48
  • That's a good example: https://developer.android.com/codelabs/android-app-permissions#5 – Martin Zeitler May 26 '22 at 07:56
  • Thank for the elaborate answer. The more you know, the more you understand the complexity of programming. I will try what you sent, appreciated – Tal Yehuda May 26 '22 at 17:56
  • [https://developer.android.com/training/data-storage](https://developer.android.com/training/data-storage) – Bö macht Blau Jun 02 '22 at 18:20

0 Answers0