简体   繁体   中英

How do I create a folder in internal storage?

I want to create a folder for my app to put audio files into. I want it to be in the same location where the DCIM folder lives. So when I run the emulator and go to Files -> Internal Storage , I want to be able to see the folder here (similar to how Snapchat creates its folder here). Is this even possible? I tried doing the below but got an error.

Code:

var path = Environment.getExternalStorageDirectory() // this is /storage/emulated/0

    
val testappDir = File(path?.absolutePath, "/testapp/")
testappDir.mkdirs() //mkdirs creates any missing parent directories, mkkdir does not
if (testappDir.createNewFile()) {
   println("File created: " + testappDir.name)
} else {
   println("File already exists.")
}

Error:

java.io.IOException: Operation not permitted

You are calling a path for an external storage with Environment.getExternalStorageDirectory() .

The Exception java.io.IOException: Operation not permitted means that you don't have the permissions to read or write the external storage.

You have to add the permission to the Android Manifest and the at Runtime you have to request the permission from the user. More information about this can be found here: https://developer.android.com/training/permissions/requesting

When you want to read and write the App Internal Storage you can found more information here: https://developer.android.com/training/data-storage/app-specific

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM