title: Android Testing: The storage state is mounted, the WRITE_EXTERNAL_STORAGE permission has been declared, but still can't write to emulator's sdcard
body: <p>I'm writing a testing program and I want to serialize some of my test result to a file. I keep on getting this annoying permission denied exception. To make things easier, I write a small sample code and still get the same permission denied warning. Here is my code, I use getExternalStorageDirectory to get the /mnt/sdcard/ directory:</p>
  
  <pre><code>private void writetry(){
          try {
              File sdCard = Environment.getExternalStorageDirectory();
              Log.d("can write", String.valueOf(sdCard.canWrite()));
              Log.d("ExternalStorageState", Environment.getExternalStorageState());
              File file = new File(sdCard, "VisitedScreen.temp");
              //file.createNewFile();
              FileOutputStream f = new FileOutputStream(file);
               byte[] buf = "Hello".getBytes();
               f.write(buf);
               f.close();
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>And here is what logcat print out:</p>
  
  <pre><code>07-12 12:28:47.288: D/can write(253): false
  07-12 12:28:47.288: D/ExternalStorageState(253): mounted
  07-12 12:28:47.297: W/System.err(253): java.io.FileNotFoundException: /sdcard/VisitedScreen.temp
  07-12 12:28:47.437: W/System.err(253):  at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
  07-12 12:28:47.437: W/System.err(253):  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:97)
  07-12 12:28:47.437: W/System.err(253):  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:69)
  </code></pre>
  
  <p>I declared my Junit testing Manifest file as: (Actually it's the wrong place to set the permission)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.mandaria.tippytipper.test"
      android:versionCode="1"
      android:versionName="1.0" &gt;
      &lt;uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" /&gt;
  
      &lt;instrumentation
          android:name="android.test.InstrumentationTestRunner"
          android:targetPackage="net.mandaria.tippytipper" /&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;uses-library android:name="android.test.runner" /&gt;
      &lt;/application&gt;
  
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Any suggestion is appreciated!</p>
  
  <p>Added: I copy the same code to a normal android program, it works, and the sdCard.canWrite() return true. But in Android Junit testing project it's not working. Anyone knows why??</p>
  
  <p>Problem solved: Thanks for all those of you have replied. It's because I haven't add the  in original Android program. I also noted that I don't need to add any permission to Junit test project. It's very interesting that the write action happened in the testing project, but permission is required in the original project. I'm wondering what if I'm doing black box testing against an .apk file. I can't change the permission from the byte code right?</p>