With Kotlin write to SD-card on Android

Viewed 1701

I have read a lot of questions and answers, but are not really satisfied and successful My Problem: write with Kotlin to a sdcard to a specific directory

Working is

        var filenamex = "export.csv"
        var patt = getExternalFilesDirs(null)
        var path = patt[1]
//create fileOut object
        var fileOut = File(path, filenamex)
//create a new file
        fileOut.createNewFile()

with getExternalFIlesDirs() I get the external storage and the sdcard. With path = patt[1] i get the adress of my sd-card.

this is

"/storage/105E-XXXX/Android/data/com.example.myApp/files"

This works to write data in this directory. But I would like to write into an other directory, for example

"/sdcard/myApp"

A lot of examples say, this should work, bit it does not. So I tried to take

"/storage/105E-XXXX/myApp"

Why doesn't it work? Ist the same beginning of storage /storage/105E-XXXX/, so it is MY sd-card.? As I mentioned, it works on the sd-card, so it is not a problem of write-permission to the sdcard?

Any idea? (I also failed with FileOutputStream and other things)

1 Answers

Try Out this..

    var filenamex = "export.csv"

    var path = getExternalStorageDirectory() + "//your desired folder"

    var fileOut = File(path, filenamex)

    fileOut.createNewFile()
Related