简体   繁体   English

如何在内部存储中创建文件夹?

[英]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.我希望它与 DCIM 文件夹所在的位置相同。 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).因此,当我运行模拟器并转到Files -> Internal Storage ,我希望能够在此处看到该文件夹​​(类似于 Snapchat 在此处创建其文件夹的方式)。 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 java.io.IOException:不允许操作

You are calling a path for an external storage with Environment.getExternalStorageDirectory() .您正在使用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.异常java.io.IOException: Operation not permitted表示您无权读取或写入外部存储。

You have to add the permission to the Android Manifest and the at Runtime you have to request the permission from the user.您必须将权限添加到 Android 清单中,并且在运行时您必须向用户请求权限。 More information about this can be found here: https://developer.android.com/training/permissions/requesting可以在此处找到有关此的更多信息: 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当您想读写应用程序内部存储时,您可以在此处找到更多信息: https : //developer.android.com/training/data-storage/app-specific

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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