Android SD card writing fails, but not consistently
<p>I've got a pretty thorny problem with Android 2.3: I have an app that gathers various logs for debugging and support purposes (<a href="http://www.sdgsystems.com" rel="nofollow">my company</a> does Linux for rugged hardware), and has stopped working lately, because it's failing to write to the SD card. Here are the symptoms I've seen and the investigations I've carried out:</p>

<ul>
<li>Happens across multiple devices of multiple types with different SD cards, all of which have been checked for filesystem corruption (no issues found).  </li>
<li>All devices report: Environment.getExternalStorageState() equals Environment.MEDIA_MOUNTED.</li>
<li>All devices also report that Environment.getExternalStorageDirectory().getAbsolutePath().canWrite() is false.</li>
<li>Via PackageManager.checkPermission(), my app reports that it has the WRITE_EXTERNAL_STORAGE permission.</li>
<li>OI File Manager is able to create directories and move files on the SD card; my app can do neither.</li>
</ul>

<p>This code is sufficient to cause a failure:</p>

<pre><code>String sdcardDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
File directory = new File(sdcardDirectory + "/logger");  
if(!directory.mkdirs()){  
    //fails here.  
    Log.w("Logger", "Could not create logger directory.");  
}
</code></pre>

<p>Since I have access to the keys for this device, I even went so far as to sign the app with the platform key and run it as android.uid.system, with no luck. Anyone have any ideas?</p>
