idish
idish

Reputation: 3260

file.mkdirs() isn't working

Here's the code:

  String folderPath = Environment.getExternalStorageDirectory() + "/AllAroundMe/Images";
      File file = new File(folderPath);
      if(!file.exists())
      {
            if(file.mkdirs());
            Log.d("MyTag","Created folders succefully");
      }
      if(file.exists())
      {
      Log.d("MyTag", "folders exists: " + file.getAbsolutePath());
      }

The second if never happens, and it should, because I make these dirs. What is wrong with my code? BTW, everytime I run this program, it always goes in the first condition.

Upvotes: 0

Views: 169

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234795

Make sure that you have

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in your android.manifest file.

Also, it would be better to construct your file object like this:

String folderPath = "AllAroundMe/Images";
File file = new File(Environment.getExternalStorageDirectory(), folderPath);

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

I think you should remove that semi-colon after that inner if: -

if(file.mkdirs()) {
    Log.d("MyTag","Created folders succefully");
}

P.S: - That is why you should always use curly braces, even if you have only single statement if, so that you don't do such kind of mistakes.

Upvotes: 1

Related Questions