title: Android App create directory works for most but gives error unable to create directory on sdcard on many
body: <p>My application creates a directory name on the phone's sd card and stores a bunch of images in it.  The code works fine for the majority of users, but some are reporting errors saving files.</p>
  
  <p>Here is the code that creates the directory:
      R.string.dir is set to "/appname/"</p>
  
  <pre><code>dir = Environment.getExternalStorageDirectory() + ((String) getResources().getText(R.string.dir));
  
  if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
      File fDir = new File(dir);
      fDir.mkdirs();
  }
  else{
      showToast(false,"This Application needs a mounted external storage (sdcard).");
      finish();
  }
  </code></pre>
  
  <p>So at this point the directory should be created (it ends up being /mnt/sdcard/appname), or the app finishes.</p>
  
  <p>However when attempting to save the file, some users are reporting this exception:</p>
  
  <blockquote>
    <p>Msg: java.io.FileNotFoundException: /mnt/sdcard/appname/file.jpg (No such file or directory)</p>
  </blockquote>
  
  <p>The code that does the saving:</p>
  
  <pre><code>File file = new File("/mnt/sdcard/appname/file.jpg");
  FileOutputStream fos = null;
  try {
      fos = new FileOutputStream( file );
      b.compress(Bitmap.CompressFormat.JPEG, 100, fos );
  }
  catch( Exception e ) {
  </code></pre>
  
  <p>Any suggestions?  Could it be that mkdirs() return false (well it would do that if the directory is already created, but then i should be able to write the file in there)?  What would be another reason for it to return false if the sdcard is mounted?  How can I handle this better?</p>
  
  <p>Yes we are using unique file names (appending
  System.currentTimeMillis() to the filename).  Also the stream is closed at the end</p>
  
  <pre><code>        finally
          {
  
          if(fos != null) {
  
          try {
  
          fos.close();
  
          }
          catch(Exception ex){
  
          ex.printStackTrace();
  
          }
  
          }
  </code></pre>