Pavel Pereverzev
Pavel Pereverzev

Reputation: 499

Creating a folder in Kotlin

I am new to Kotlin and have read lots of tutorials, tried bunches of code but still can't understand how to create a folder in internal storage.

I need to create a folder in which I wil put a json resource file.

Manifest file contains <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> and <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

My code sample is:

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

        val folder = File(
            Environment.getDataDirectory().toString() + separator.toString() + "MetroPol"
        )
        if (folder.exists()) {
            d("folder", "exists")
        } else {
            d("folder", "not exists")
            folder.mkdirs()
        }
    }

I test it using my phone connected to a pc and recognised by Android Studio. When this app launches I go to a browser and don't see any new folder.

What should be done here?

Upvotes: 11

Views: 18610

Answers (5)

M Nabeel
M Nabeel

Reputation: 1

Also try this

val relativeLocationPictures =
                    Environment.DIRECTORY_DCIM + File.separator + "GPS camera app" + File.separator + "Pictures" + File.separator + folderName
                val folder = File(relativeLocationPictures)
                if (!folder.exists()) {
                    if (folder.mkdirs()) {
                        // Folder created successfully
                    } else {
                        // Failed to create the folder
                    }
                } else {
                    // Folder already exists
                }

Upvotes: 0

Saeed Ho
Saeed Ho

Reputation: 1

val appDirctory =File(Environment.getExternalStorageDirectory().path + "/test")
appDirctory.mkdirs()

Upvotes: 0

Mayank Bhatnagar
Mayank Bhatnagar

Reputation: 2191

To create a folder inside your Internal Storage, try out this code snippet

val folder = filesDir
val f = File(folder, "folder_name")
f.mkdir()

Finally to check if the folder is created open Device Explorer in Android Studio, then follow the path

data->data->your app package name -> files-> here should be your folder that you created programmatically. Hope this helps

Upvotes: 13

mrtcnkryln
mrtcnkryln

Reputation: 324

Can you try this ?

class MainActivity() : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var filename = "test.txt"
        val folder = File("/sdcard/MetroPol/")
        folder.mkdirs()
        val outputFile = File(folder, filename)
        try {
            val fos = FileOutputStream(outputFile)
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        }
    }

Upvotes: -1

CommonsWare
CommonsWare

Reputation: 1006799

I am new to Kotlin and have read lots of tutorials, tried bunches of code but still can't understand how to create a folder in internal storage.

It seems as though you really want to be creating a directory in external storage.

Since that is no longer being supported on Android 10 (by default) and Android R+ (for all apps), I recommend that you let the user create the directory themselves, and you get access to it via ACTION_OPEN_DOCUMENT_TREE and the Storage Access Framework.

When this app launches I go to a browser and don't see any new folder.

The root of external storage is Environment.getExternalStorageDirectory().

Upvotes: 0

Related Questions