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 trial

Android Build a Self-Destructing Message Android App Capturing Photos and Videos Setting Where Photos are Saved

When 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
Seth Kroger
56,413 Points

From the docs, mkdir() makes the directory and returns a boolean indicating if it succeeded or not.

Yes but then shouldn't we have

if (! mediaStorageDir.exists()) {
                if (! mediaStorageDir.mkdir()) { // when does it make it then?
                    mediaStorageDir.mkdir() // added code
                    Log.e(TAG, "Failed to create directory");
                    return null;
                }
Seth Kroger
Seth Kroger
56,413 Points

I 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;
   }
}

I'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
Seth Kroger
56,413 Points

The 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
Seth Kroger
56,413 Points

The 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.

Okay 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!