title: Android mkdirs() creates a zero byte file instead of a folder
body: <p>In my android application, I am trying to create the following folder on the sdcard:</p>
  
  <pre><code>/mnt/sdcard/OSGiComponents/admin/felix-cache/
  </code></pre>
  
  <p>Here's the code:</p>
  
  <pre><code>File cacheDir = 
      new File( Environment.getExternalStorageDirectory().getAbsolutePath() + 
                "/OSGiComponents/admin/felix-cache/" );
  // Create the folder
  cacheDir.mkdirs();
  // Check if it exists
  if ( ! cacheDir.exists() ) {
      Log.e ( "Debug" , "Cache directory cannot be created" );
  }
  </code></pre>
  
  <p>I have the WRITE_STORAGE_PERMISSION under the manifest tag of the android manifest file. I am able to create other folders and files without problem on the sdcard.
  The app works fine on the following phones:</p>
  
  <ol>
  <li>Nexus S (rooted) running Gingerbread (2.3)</li>
  <li>Nexus S (unrooted) running Jelly Bean (4.1.2)</li>
  <li>HTC Desire (rooted) running Froyo (2.2)</li>
  <li>HTC Desire (unrooted) running Froyo (2.2)</li>
  </ol>
  
  <p>However on Samsung Galaxy Nexus phone (unrooted) running Ice Cream Sandwich (4.0.4), the directory is created as a zero size file, which can be seen in Astro. The exists() call returns false. </p>
  
  <p><img src="https://i.stack.imgur.com/7BEyd.png" alt="Astro showing a zero byte felix-cache file"></p>
  
  <ol>
  <li>As you can see from the folder name, I am using Apache Felix. Felix creates the cache directory automatically if it does not exist. On Galaxy Nexus, it always complained that it is unable to create the cache directory. Astro shows a 0 byte file instead of a folder. This is why I decided to try creating the cache folder myself before initializing Felix.</li>
  <li>So, I create the cache folder myself. The app works fine the first time, and I can see the folder fine in Astro. If I close the app, then delete the folder in Astro, and then re-launch the app, even my code mysteriously cannot create the cache directory, and Astro shows a 0 byte file.</li>
  <li>The 0 byte file cannot be deleted in Astro. However, when I reboot the phone, the folder is magically there and ok.</li>
  <li>I use FileInstall to watch the OSGiComponents/install folder. When I drop bundle jars into that folder, it is detected and installed ok on all phones except Galaxy Nexus (when the app works the first time). There are no logs/errors from FileInstall about not being able to watch the directory.</li>
  <li>I have tested this on 2 Galaxy Nexus phones, same problem. </li>
  </ol>
  
  <p>I suspect it is a permissions problem, but I not sure what it is, and why a 0 byte file is created while exists() returns false. Nowhere else in the code am I creating this file.</p>
  
  <p>Any suggestions on what could be the problem?</p>
  
  <p>Thanks :)</p>
  
  <p>UPDATE: I think I have identified the issue, please see the answer I posted.</p>