即使在测试用例方法中(在ActivityInstrumentationTestCase2
的子类内),我也无法写入外部存储器:
public void testWriteToStorage() throws Throwable {
File file = new File(Environment.getExternalStorageDirectory(), "test");
new FileOutputStream(file); // error thrown here
}
错误:
java.io.FileNotFoundException: /storage/emulated/0/test: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at com.unacademy.activity.GalleryTestCase.testWriteToStorage(GalleryTestCase.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 32 more
我在清单中拥有WRITE_EXTERNAL_STORAGE
权限,我知道在API 23之后 dangerous 权限需要用户在运行时同意。
但这不在Application
上下文中;它在仪器环境中。此外,它适用于AWS device farm sample app。我尽我所能从那里复制完全相同的代码,但它仍然在这里失败。
注意:我只是尝试截取并存储在外部存储中,以便AWS Device Farm接收它。如果有人设法以其他方式做到这一点,请告诉我;这也将非常有用。
答案 0 :(得分:1)
我想到的一件事是你的目标是M并且可能没有在运行时处理权限请求。
正如您所说,M WRITE_EXTERNAL_STORAGE
被视为危险权限,但权限处理也发生了变化。在M之前,在清单中声明了权限,并在安装应用程序时由用户接受。在M中,您还必须在运行时请求权限。
请检查此库,以帮助您处理M。
中的权限https://github.com/aitorvs/allowme
我还建议您尝试尽可能避免外部存储,甚至更好地避免存储权限。查看Ian Lake的精彩演讲。
答案 1 :(得分:0)
我通过提供存储权限使其工作。终端(在运行测试用例之前)像
adb shell pm grant com.example.abhishek.test android.permission.WRITE_EXTERNAL_STORAGE