android - 如何在 Android 4.4.2(Kitkat) API 级别 19 的外部 SD 卡中创建文件夹

标签 android directory android-sdcard

我尝试了很多在 android 4.4.2 版本的 SD 卡中创建一个文件夹。 我试试下面的代码

 String dirName="Test";
   File file = new File(Environment.getExternalStorageDirectory(), dirName);
        boolean status = file.mkdir();
                    if (status)
                        Toast.makeText(MainActivity.this,
                                "Directory created successfully", Toast.LENGTH_SHORT)
                                .show();
                        else
                            Toast.makeText(MainActivity.this, "Directory create failed",
                                    Toast.LENGTH_SHORT).show();

但它会在内部存储中创建一个文件夹。

我尝试另一个代码是

  String path=System.getenv("SECONDARY_STORAGE");
                File file=new File(path +"/Test123");
                file.mkdir();

它在 Android 4.1 的 External SDCARD 中创建了一个文件夹,但在 Android 4.4.2 中没有

那么我如何在 android 4.4.2 的外部 sdcard 中创建一个文件夹? 如果有人知道请帮助我..

提前致谢

最佳答案

文档指出

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled.

哪里有 top-level directory它们表示 Enviroment.getExternalStorageDirectory() 的返回值

使用 getExternalFilesDir你会有

 File file = new File (getExternalFilesDir(null), dirName);
 if (!file.exists()) {
   boolean status = file.mkdir();
   if (status) {
        Toast.makeText(MainActivity.this, "Directory created successfully", Toast.LENGTH_SHORT).show();
   } else {
        Toast.makeText(MainActivity.this, "Directory create failed", Toast.LENGTH_SHORT).show();
   }
 }

你还应该知道 mkdir()如果目录已经存在则返回 false

关于android - 如何在 Android 4.4.2(Kitkat) API 级别 19 的外部 SD 卡中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33051797/

相关文章:

c# - 如何将 List<System.IO.FileInfo> 的类型转换为 List<string>?

c# - 在 Directory.GetFiles() 中停止隐式通配符

Linux:查找文件时使用文件夹名循环

java - 图像无法识别

android - java.Lang.RuntimeException, setParameters 在android(4.1.1) 版本中失败

android - 在选项卡布局中添加 gridview

android - websocket 是否专用于浏览器

android - 如何在发布版本中使用 Zebra EMDK?

java - Android Studio Gradle 同步失败 : Unable to start the daemon process

android - 定期删除外部缓存目录