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");
}
in log i am getting:
False
False
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
Please help
Thank You
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