【发布时间】:2015-11-07 04:31:46
【问题描述】:
在有人将此标记为重复之前,我想让您知道,我已经解决了很多关于 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 权限。
这是我的代码 sn-p:
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");
}
}
【问题讨论】:
-
你用过什么api版本? 2.3 还是 18+?
-
我在 LG G2 上使用 5.0.2