Android的测试:存储状态下安装时,WRITE_EXTERNAL_STORAG​​E许可已经声明,但仍不能写入模拟器的SD卡 [英] Android Testing: The storage state is mounted, the WRITE_EXTERNAL_STORAGE permission has been declared, but still can't write to emulator's sdcard

查看:379
本文介绍了Android的测试:存储状态下安装时,WRITE_EXTERNAL_STORAG​​E许可已经声明,但仍不能写入模拟器的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个测试程序,我想序列我的一些测试结果到一个文件中。我一直得到这恼人的许可被拒绝例外。为了方便起见,我写了一个小样本code和仍然得到同样的许可被拒绝警告。这里是我的code,我用getExternalStorageDirectory得到的/ mnt / SD卡/目录:

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();
        }
    }

和这里是logcat的打印出来:

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)

我宣布我的JUnit进行测试清单文件(其实这是错误的地点设置权限)

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>

任何建议AP preciated!

Any suggestion is appreciated!

补充:我在同一code复制到一个正常的Andr​​oid程序,它的工作原理,以及sdCard.canWrite()返回true。但在Android的JUnit进行测试的项目它不工作。任何人都知道这是为什么?

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??

问题解决了:感谢所有那些你已经回答。这是因为我没有在原生Android程序会添加。我也注意到,我不需要任何许可添加到JUnit测试项目。这是非常有趣的是,写入操作在测试项目中发生的事情,但允许在原有项目所需。我想知道如果我在做什么针对.apk文件黑盒测试。我不能同意从字节code正确的改变?

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?

推荐答案

在使用许可权

<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.

这篇关于Android的测试:存储状态下安装时,WRITE_EXTERNAL_STORAG​​E许可已经声明,但仍不能写入模拟器的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆