Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,405 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,405 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,405 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,405 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!