Why is the mkdirs() method not working?

…衆ロ難τιáo~ 提交于 2019-12-05 22:45:43

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");
                }

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!