问题描述
这是code我使用创建的默认文件夹的图片文件夹:
This is the code i am using to create a folder in the default pictures folder:
File imagesFolder = new File(Environment.DIRECTORY_PICTURES, "/images");
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 1", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 1", "True");
}
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 2", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 2", "True");
}
在日志中我得到:
False
False
在第1次目录不是present,因此假
,但随即我使用 mkdirs(),因此,我希望第二个日志是
真
但即使是假
和我的应用程序崩溃因为 NullPointerException异常
在code的后期
for the 1st time the directory is not present, hence False
but then immediately i am creating it using mkdirs()
, hence i expect the 2nd log to be True
but even that is False
and my application crashed because of NullPointerException
in the later part of the code
请帮忙
感谢您
推荐答案
您正在使用 Environment.DIRECTORY_PICTURES
走错了路。这只是一个字符串
如图片
而不是一个路径不变。你需要得到通过的路径 Environment.getExternalStoragePublicDirectory(串)
You are using Environment.DIRECTORY_PICTURES
the wrong way. It's just a String
constant like "Pictures"
but not a path. You need to get the path via Environment.getExternalStoragePublicDirectory(string)
File pictureFolder = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
);
File imagesFolder = new File(pictureFolder, "images");
// etc
这篇关于安卓:无法创建默认图片文件夹目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!