Android Testing: The storage state is mounted, the WRITE_EXTERNAL_STORAGE permission has been declared, but still can't write to emulator's sdcard

余生长醉 提交于 2019-12-10 11:51:42

问题


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?


回答1:


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.




回答2:


Change this line:

File sdCard = Environment.getExternalStorageDirectory();

to this:

File sdCard = new File(Environment.getExternalStorageDirectory().getPath());



回答3:


I ran into the same problem. To fix this I not only had to add the permission tag to the Main manifest, but I also had to add the tag to the manifest of the module containing the unittest.

Permission tag:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


来源:https://stackoverflow.com/questions/11459401/android-testing-the-storage-state-is-mounted-the-write-external-storage-permis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!