6

This is my code

File selfieLocation = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
               "Daily Selfies");
boolean isDirectory = false;
if(!selfieLocation.isDirectory()) {
        //Creates directory named by this file
        selfieLocation.mkdir();
        isDirectory = selfieLocation.isDirectory();
 }
 //array of strings 
 for(String selfiePath: selfieLocation.list()) {
        selfies.add(selfiePath);
 }

Basically what I am trying to do is create my own customizable directory inside of the standard directory in which to place pictures that are available to the user.

I looked at related threads and saw this one, Android: unable to create a directory in default pictures folder. However I made sure that I had a call to getExternal...., and not just have Environment.DIRECTORY_PICTURES as a parameter. I also looked on here http://developer.android.com/guide/topics/data/data-storage.html#filesExternal and saw that I had the right method call/format to create a customizable folder in external memory. The docs example was

File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
    Log.e(LOG_TAG, "Directory not created");
}

I stepped through my code and saw that the local variable isDirectory stayed at false even after the call to selfieLocation.mkdir(). Does anyone know why this directory cannot be created?

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

5

Try to create directory with File#mkdirs(), not File#mkdir(). The latter assumes that all parent directories are already in place, so it won't create directory if any of its parents don't exist.
Also, take a look at your permissions in AndroidManifest.xml. You need the following permissions in order to read/write the content on external storage:

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

android.permission.READ_EXTERNAL_STORAGE isn't required for now, but it will be in the future releases of Android.

aga
  • 27,954
  • 13
  • 86
  • 121
  • the permission isn't the issue because I have that and didn't get a security exception. mkdirs didn't work either. The parent does exist in this case (getExternalFilesDir(Environment.DIRECTORY_PICTURES) – committedandroider Nov 27 '14 at 08:28
  • By the way, can you give this question a up vote if you think its a good question? I am in danger of a question ban so I try to make sure that I ask good questions with research from now on. – committedandroider Nov 27 '14 at 08:29
  • I've tried to reproduce your error and everything works just fine on the emulator. The issue has to be somewhere else. I give an upvote whenever I see a question that may be interesting to me. That's good question, with research effort, but it isn't intersting from my point of view. – aga Nov 27 '14 at 09:10
  • Yeah the issue was that my phone's external media isn't even mounted when i plug it into usb of computer. That's frustrating – committedandroider Nov 27 '14 at 16:15