在任何人将其标记为副本之前,我想让大家知道,我已经在SO上经历了很多关于mkdirs()方法的问题,而且没有一个问题对我有用,所以我相信对于这个问题,我有一个特别的例子值得问。
我尝试使用mkdir(),将目录File的实例化更改为
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");
}
}发布于 2015-08-14 03:14:23
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");
}https://stackoverflow.com/questions/32001235
复制相似问题