【问题标题】:Android mkdirs() sdcard do not workAndroid mkdirs() sdcard 不工作
【发布时间】:2014-03-19 03:15:02
【问题描述】:

我想在 SD 卡中创建目录,我确实遵循:

  1. 我在清单中添加了:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  2. 我通过:public static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + "/Hello_World/"; 得到 root_path,它返回 /storage/emulated/0/Hello_World(调试时获取)。

接下来,我运行这段代码:

File file = new File(Constants.ROOT_PATH);
int i = 0;
while (!file.isDirectory() && !file.mkdirs()) {
    file.mkdirs();
    Log.e("mkdirs", "" + i++);
}

我也尝试了mkdirs()mkdir(),但它在 logcat (Log.e("mkdirs", "" + i++);) 中显示无限循环。有时它起作用,但有时不起作用。 谢谢你的帮助!
Update:我在一些设备上尝试了我的代码:Nexus4、nexus7、Vega Iron、Genymotion、LG G Pro,然后只是 Vega Iron 按预期工作。 ??!!?!?

【问题讨论】:

  • 运行您的代码以将其安装在设备中。现在从计算机上拔下并运行您的应用程序。代替 Log.e 显示敬酒。
  • @HoanNguyen 我试过但没有帮助:(
  • 您必须更具体地了解遇到的问题。将这个“logcat 中的无限循环”的示例编辑到您的问题中。
  • 使用设备设置清除您的应用数据,然后再次尝试我上面评论的内容。
  • @ChrisStratton 由Log.e("mkdirs", "" + i++); 命令引起。 @Hoan Nguyen:我之前尝试过清理数据、重新安装应用程序、更换设备(nexus4、nexus7、vega Iron、genymotion),但仍然无法正常工作:(

标签: android mkdirs


【解决方案1】:

像这样尝试它会在sd card中创建一个文件夹

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/hello_world");    
myDir.mkdirs();

如果您想检查该文件是否存在,请使用此代码

File file = new File (myDir, file_name);
if (file.exists ()) 
   // file exist 
else 
   // file not exist  

参考看看这个答案Android saving file to external storage

【讨论】:

  • 我想知道你的解决方案和我的有什么不同? //还是不行 :(
  • 它会在 sd 卡中创建一个文件夹 .. 那你期待什么?
  • 我是我的代码,我也尝试在 sdcard 上创建一个名为 Hello_World 的文件夹,有时可以,有时不行:(
  • file.isDirectory()file.mkdirs() 都返回 false,所以我的 while() 无限循环。
  • 你真的检查过我的代码吗..???您没有正确创建文件。因为它返回错误.. File file = new File (myDir, file_name);首先创建文件夹,然后检查该文件夹是否为文件。在我的答案或参考链接中正确检查代码..
【解决方案2】:

错误是由&& 中的while (!file.isDirectory() && !file.mkdirs()) 引起的,它应该是while (!file.isDirectory() || !file.mkdirs())。您还应该检查媒体是否已安装。

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        if (DEBUG) {Log.d(TAG, "createSoundDir: media mounted");} //$NON-NLS-1$
        File externalStorage = Environment.getExternalStorageDirectory();
        if (externalStorage != null)
        {
            String externalStoragePath = externalStorage.getAbsolutePath();
            File soundPathDir = new File(externalStoragePath + File.separator + "Hello_World"); //$NON-NLS-1$

            if (soundPathDir.isDirectory() || soundPathDir.mkdirs())
            {
                String soundPath = soundPathDir.getAbsolutePath() + File.separator;
                if (DEBUG) {Log.d(TAG, "soundPath = " + soundPath);} //$NON-NLS-1$

            }
        }
    }

从我的一个项目中剪切和粘贴。

【讨论】:

    【解决方案3】:

    谢谢大家,终于找到问题所在了。问题出在while() 循环中,我替换为

    如果 (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && !file.isDirectory()) {
    文件.mkdirs();
    }

    【讨论】:

    • 问题的一部分可能是 mkdirs() 仅在创建目录时返回 true,而不是在目录已存在时返回。
    【解决方案4】:

    如下使用Environment.getExternalStorageDirectory().getAbsolutePath()...

    public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hello_World/";
    

    并在创建目录之前检查 SDCard 是否已挂载,如下所示....

    File file = new File(Constants.ROOT_PATH);
    int i = 0;
    
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
          if(!file.exists()) {
              file.mkdir();
          }
    }
    

    【讨论】:

    • 感谢您的回答,但它仍然返回 /storage/emulated/0/Hello_World 并且仍然不起作用。顺便说一句,我想知道你为什么编辑我的帖子(我很难阅读)并 -1 我?再次感谢!
    • 我看到并改变了,但没有任何改变:(
    猜你喜欢
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2017-09-22
    • 2016-03-11
    • 2018-03-16
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多