简体   繁体   English

如何在android中创建一个新文件夹?

[英]How to create a new folder in android?

I want to create a new folder in external storage. 我想在外部存储中创建一个新文件夹。 I use this code: 我使用以下代码:

            val folderMain = "name"

        val f = File(getExternalStorageDirectory(), folderMain)
        if (!f.exists()) {
            f.mkdirs()
        }

After executing it works and creates a new folder in internal storage, not in external storage. 执行后,它将在内部存储(而不是外部存储)中创建并创建一个新文件夹。 How can I create a new folder in external storage? 如何在外部存储中创建新文件夹?

I am 100% sure I have external storage in my device and it has 14 GB space available in it and location is /storage/extSdCard . 我100%确定设备中有外部存储,并且其中有14 GB可用空间,位置为/storage/extSdCard

I have tested this code on two Samsung phones Android version jellybean and Kitkat but the same result. 我已经在两部三星手机Android版本的jellybean和Kitkat上测试了此代码,但结果相同。

You can have the answers to your questions here : 您可以在这里找到问题的答案:

https://developer.android.com/training/data-storage https://developer.android.com/training/data-storage

I also recommend you to check the result of mkdirs() : 我也建议您检查mkdirs()的结果:

if (!f.exists() && !f.mkdirs()) {
    Log.e("mDebug", "Couldn't create " + f.getName());
}

After executing it works and creates a new folder in internal storage, not in external storage. 执行后,它将在内部存储(而不是外部存储)中创建并创建一个新文件夹。

No, it creates the file in what the Android SDK refers to as external storage . 不,它将在Android SDK称为外部存储的文件中创建文件。

I am 100% sure I have external storage in my device and it has 14 GB space available in it and location is /storage/extSdCard. 我100%确定设备中有外部存储,并且其中有14 GB可用空间,位置为/ storage / extSdCard。

That represents removable storage . 那代表可移动存储

How can I create a new folder in external storage? 如何在外部存储中创建新文件夹?

Your existing code creates a directory in the root of external storage. 您现有的代码在外部存储的根目录中创建一个目录。 You have no ability to write to arbitrary locations on removable storage. 您无法写入可移动存储中的任意位置。

The simplest places to write to on removable storage are in the locations supplied by the getExternalFilesDirs() , getExternalCacheDirs() , and getExternalMediaDirs() methods on a Context . 最简单地写入可移动存储在由所提供的位置getExternalFilesDirs() getExternalCacheDirs()getExternalMediaDirs()上的方法Context If those return 2+ locations, the second and subsequent ones are on removable storage, and you have full read/write access to them. 如果这些返回的位置超过2个,则第二个及后续位置位于可移动存储中,并且您具有对它们的完全读取/写入权限。

try this code: 试试这个代码:

String fileName = "myfile.zip";
    File ZIP_Directory = new File("/sdcard/MOBstate/");
    ZIP_Directory.mkdirs();        
     File outputFile = new File(PDF_Directory, fileName);
     FileOutputStream fos = new FileOutputStream(outputFile);

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

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