找到你要的答案

Q:Why is the mkdirs() method not working?

Q:的mkdirs()方法为什么不工作?

Before anyone marks this as a duplicate I would like to let you know that I have gone through a lot of the questions about the mkdirs() method on SO and none have worked for me so I believe I have a special case for this problem worthy of a question.

I have tried using mkdir(), changed the instantiation of the directory File as

new File(Environment.getExternalStorageDirectory())
new File(Environment.getExternalStorageDirectory().getAbsolutePath())
new File(Environment.getExternalStorageDirectory(), "Directory")
new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Directory")
new File(Environment.getExternalStorageDirectory().toString + "/Directory")

and nothing works.

NOTE : I ALSO HAVE THE WRITE_EXTERNAL_STORAGE permission in my manifest.

Here is my code snippet:

File rootDirectory = new File(Environment.getExternalStorageDirectory(), getActivity().getPackageName());

File directory = new File(rootDirectory, "Directory");
if (!directory.exists()) {
    if(directory.mkdirs()){
       Log.d("log", "exists");
    } else {
       Log.d("log", "not exists");
    }
 }

在任何人都标志着这是一个重复的我想让你知道,我经历了很多关于这样一mkdirs()方法等问题,为我工作,所以我相信我对这个问题有一个问题值得特殊情况。

我试图用mkdir(),改变目录文件实例化为

new File(Environment.getExternalStorageDirectory())
new File(Environment.getExternalStorageDirectory().getAbsolutePath())
new File(Environment.getExternalStorageDirectory(), "Directory")
new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "Directory")
new File(Environment.getExternalStorageDirectory().toString + "/Directory")

没有工作。

注:我也有我的体现write_external_storage许可。

这里是我的代码段:

File rootDirectory = new File(Environment.getExternalStorageDirectory(), getActivity().getPackageName());

File directory = new File(rootDirectory, "Directory");
if (!directory.exists()) {
    if(directory.mkdirs()){
       Log.d("log", "exists");
    } else {
       Log.d("log", "not exists");
    }
 }
answer1: 回答1:

mkdirs create the complete folder tree, mkdir only the last folder in complete folder tree path

use a code like this:

String foldername = "HelloWorld";
                 File dir = new File(Environment.getExternalStorageDirectory() + "/" + foldername);
                if (dir.exists() && dir.isDirectory()) {
                    Log.d("log", "exists");
                } else {
                    //noinspection ResultOfMethodCallIgnored
                    dir.mkdir();
                    Log.d("log", "created");
                }

mkdirs创建完整的文件夹树,mkdir只有最后的文件夹完整的文件夹树的路径

使用这样的代码:

String foldername = "HelloWorld";
                 File dir = new File(Environment.getExternalStorageDirectory() + "/" + foldername);
                if (dir.exists() && dir.isDirectory()) {
                    Log.d("log", "exists");
                } else {
                    //noinspection ResultOfMethodCallIgnored
                    dir.mkdir();
                    Log.d("log", "created");
                }
answer2: 回答2:

use this

File rootDirectory = new File(Environment.getExternalStorageDirectory(), 
      new File(Environment.DIRECTORY_DOCUMENTS, getActivity().getPackageName()));
   // if you target below api 19 use this => DIRECTORY_DOWNLOADS
  // now your old code follows life is good
File directory = new File(rootDirectory, "Directory");
if (!directory.exists()) {
    if(directory.mkdirs()){
       Log.d("log", "exists");
    } else {
       Log.d("log", "not exists");
    }
 }

more info here and here

from your comment

But I don't want the users to be able to see the files I am going to save into the folder

you could use internal memory and call this [getDir(java.lang.String, int)](http://developer.android.com/reference/android/content/Context.html#getDir(java.lang.String, int))

let me know if it helps sir

使用此

File rootDirectory = new File(Environment.getExternalStorageDirectory(), 
      new File(Environment.DIRECTORY_DOCUMENTS, getActivity().getPackageName()));
   // if you target below api 19 使用此 => DIRECTORY_DOWNLOADS
  // now your old code follows life is good
File directory = new File(rootDirectory, "Directory");
if (!directory.exists()) {
    if(directory.mkdirs()){
       Log.d("log", "exists");
    } else {
       Log.d("log", "not exists");
    }
 }

这里和这里的更多信息

从你的评论

但我不希望用户能够看到我要保存到文件夹中的文件

you could use internal memory and call this [getDir(java.lang.String, int)](http://developer.android.com/reference/android/content/Context.html#getDir(java.lang.String, int))

让我知道,如果它帮助先生

android  file  mkdirs