2

I want to create a folder in the internal storage of an android device where I can save my .csv files. But I am unable to create a folder in the internal storage.

Below is my code snippet for creating the folder

fun getStorageDirectory(): File {
    if (Environment.getExternalStorageState() == null) {
        val f = File(Environment.getDataDirectory().absolutePath + "/Afolder/")
        if (!f.exists())
            f.mkdirs()
        return f
    } else {
        val f = File(Environment.getExternalStorageDirectory().absolutePath + "/Afolder/")
        if (!f.exists())
            f.mkdirs()
        return f
    }
}

and this is the permissions I used in my manifest.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Arnolfo Teope
  • 73
  • 2
  • 10
  • But did you get the *runtime permission* that allows you to write outside the world? – EpicPandaForce Mar 14 '19 at 02:02
  • no sir i am new to android programming how can i do that? – Arnolfo Teope Mar 14 '19 at 02:05
  • 1
    Just use https://stackoverflow.com/a/43322136/2413303 (specifically `checkPermissionForExternalStorage()` and `requestPermissionForExternalStorage`) – EpicPandaForce Mar 14 '19 at 02:08
  • return if (Environment.getExternalStorageState() == null) { File(Environment.getDataDirectory().absolutePath + pathMedia).apply { if (!this.exists()) mkdirs() } } else { File(Environment.getExternalStorageDirectory().absolutePath + pathMedia).apply { if (!this.exists()) mkdirs() } } – Lerb90 Oct 30 '19 at 10:35

0 Answers0