Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Partridge
7,129 PointsWhen are we making the Directory?
// 2. Create our subdirectory
if (! mediaStorageDir.exists()) {
if (! mediaStorageDir.mkdir()) { // when does it make it then?
Log.e(TAG, "Failed to create directory");
return null;
}
}
It seems that we are only checking the boolean and not making the dir. When is the dir being made?
3 Answers
Seth Kroger
56,413 PointsFrom the docs, mkdir() makes the directory and returns a boolean indicating if it succeeded or not.
Seth Kroger
56,413 PointsI think I see the issue. Whatever is inside the if statement (or while, for, etc) is still code and will still be executed.
if (! mediaStorageDir.exists()) { // Does the directory exist? If not then...
if (! mediaStorageDir.mkdir()) { // ...Make the directory. If not successful...
Log.e(TAG, "Failed to create directory"); // ...Log the failure
return null;
}
}
Michael Partridge
7,129 PointsI'm still a little confused. In plain english this block would read to me: "If 'mediaStorageDir.exists()' is not true then if 'mediaStorageDir.mkdir()' is not true."
You see where I'm getting confused lol! That's why it made sense to add code inside the second if statement. Maybe there's more to the 'mkdir()' method.
What does this block of code read to you?
Seth Kroger
56,413 PointsThe comments in the code above tell you how I read it. Another way to look at it is:
if (! mediaStorageDir.mkdir()) {
Log.e(TAG, "Failed to create directory");
}
// Is the same as
boolean success = mediaStorageDir.mkdir();
if (!success) {
Log.e(TAG, "Failed to create directory");
}
You're simply skipping a redundant variable.
Seth Kroger
56,413 PointsThe entirety of the docs I already linked to:
public boolean mkdir ()
Added in API level 1
Creates the directory named by this file [emphasis mine], assuming its parents exist. Use mkdirs() if you also want to create missing parents.
Note that this method does not throw IOException on failure. Callers must check the return value. Note also that this method returns false if the directory already existed. If you want to know whether the directory exists on return, either use (f.mkdir() || f.isDirectory()) or simply ignore the return value from this method and simply call isDirectory().
Returns
true if the directory was created, false on failure or if the directory already existed.
Michael Partridge
7,129 PointsOkay it's cleared up. Thank you for your patience and clarity! It took a little bit for me to understand, but once I acknowledged that the code in the if condition is still run (as you mentioned) it all makes sense. Thanks again! Happy Coding!
Michael Partridge
7,129 PointsMichael Partridge
7,129 PointsYes but then shouldn't we have