为什么mkdirs()方法不工作? [英] Why is the mkdirs() method not working?

查看:787
本文介绍了为什么mkdirs()方法不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何人都标志着这是一个重复的,我想告诉你,我已经经历了很多关于 mkdirs问题()方法转眼就SO和无已经工作了我,所以我相信我有这个问题值得一问的一个特例。

我一直在使用的mkdir()试图,改变了目录的实例文件

 新建文件(Environment.getExternalStorageDirectory())
新的文件(Environment.getExternalStorageDirectory()。getAbsolutePath())
新的文件(Environment.getExternalStorageDirectory(),目录)
新的文件(Environment.getExternalStorageDirectory()。getAbsolutePath(),目录)
新的文件(Environment.getExternalStorageDirectory()。的toString +/目录)

和没有什么作品。

请注意:我也有我的清单中的 WRITE_EXTERNAL_STORAG​​E 许可

下面是我的code片断:

 文件根目录=新的文件(Environment.getExternalStorageDirectory(),getActivity()getPackageName());文件目录=新的文件(根目录,目录);
如果(!directory.exists()){
    如果(directory.mkdirs()){
       Log.d(日志,存在);
    }其他{
       Log.d(日志,不存在);
    }
 }


解决方案

mkdirs创建完整的文件夹树,MKDIR只有在完整的文件夹树路径中的最后一个文件夹

使用code是这样的:

 字符串文件夹名=HelloWorld的;
                 文件DIR =新的文件(Environment.getExternalStorageDirectory()+/+ FOLDERNAME);
                如果(dir.exists()及&放大器; dir.isDirectory()){
                    Log.d(日志,存在);
                }其他{
                    // noinspection ResultOfMethodCallIgnored
                    dir.mkdir();
                    Log.d(日志,创造);
                }

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 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()方法不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆