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:
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();
}
}
And here is what logcat print out:
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.<init>(FileOutputStream.java:97)
07-12 12:28:47.437: W/System.err(253): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
I declared my Junit testing Manifest file as: (Actually it's the wrong place to set the permission)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.mandaria.tippytipper.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="net.mandaria.tippytipper" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Any suggestion is appreciated!
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??
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?
The uses-permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
should be added to your main project's manifest. Remember that tests are run in the same process as the main application.