UiAutomator2 Android Studio模拟器使用takescreenshot()不能成功截图

FunctionTwo 2017-04-13 10:33:07

@RunWith(AndroidJUnit4.class)
public class BySelectorTest {
UiDevice device;
Instrumentation instrumentation;

@Before
public void setUp() throws RemoteException, UiObjectNotFoundException {
//初始化
instrumentation= InstrumentationRegistry.getInstrumentation();
device=UiDevice.getInstance(instrumentation);

@Test
public void testByselectorBy() throws UiObjectNotFoundException, RemoteException {



Boolean tmpa = device.takeScreenshot(new File("/mnt/sdcard/DCIM/test2.png"));
System.out.println("截屏返回值:" + tmpa);





04-13 14:24:13.428 5144-5158/? W/gralloc_ranchu: Gralloc pipe failed

[ 04-13 14:24:13.428 5144: 5158 D/ ]
HostConnection::get() New Host Connection established 0xa0738180, tid 5158
04-13 14:24:13.457 1229-1229/? E/EGL_emulation: tid 1229: eglCreateSyncKHR(1451): error 0x3004 (EGL_BAD_ATTRIBUTE)
04-13 14:24:13.547 5160-5184/? E/UiAutomatorBridge: failed to save screen shot to file
java.io.FileNotFoundException: /data/local/tmp/test2.png (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at android.support.test.uiautomator.UiAutomatorBridge.takeScreenshot(UiAutomatorBridge.java:131)
at android.support.test.uiautomator.UiDevice.takeScreenshot(UiDevice.java:1025)
at android.support.test.uiautomator.UiDevice.takeScreenshot(UiDevice.java:1009)
at BySelectorTest.testByselectorBy(BySelectorTest.java:71)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.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:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
04-13 14:24:13.548 5160-5184/? I/System.out: 截屏返回值:false
04-13 14:24:13.549 5160-5184/? I/TestRunner: finished: testByselectorBy(BySelectorTest)
04-13 14:24:13.553 5160-5160/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
04-13 14:24:13.553 5160-5184/? I/TestRunner: started: useAppContext(com.example.administrator.ui2byselector.ExampleInstrumentedTest)
04-13 14:24:13.554 5160-5160/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
04-13 14:24:13.555 5160-5184/? I/TestRunner: finished: useAppContext(com.example.administrator.ui2byselector.ExampleInstrumentedTest)
04-13 14:24:13.556 5160-5160/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
04-13 14:24:13.558 5160-5184/? I/TestRunner: run finished: 2 tests, 0 failed, 0 ignored
04-13 14:24:13.559 5160-5184/? I/MonitoringInstrumentation: waitForActivitiesToComplete() took: 0ms
04-13 14:24:13.560 5160-5160/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
04-13 14:24:13.560 1489-1503/system_process W/WindowManager: Attempted to remove non-existing token: android.os.Binder@27006d6
04-13 14:24:13.564 1489-1887/system_process I/ActivityManager: Force stopping com.example.administrator.ui2byselector appid=10075 user=0: finished inst
04-13 14:24:13.564 1489-1887/system_process I/ActivityManager: Killing 5160:com.example.administrator.ui2byselector/u0a75 (adj 0): stop com.example.administrator.ui2byselector
04-13 14:24:13.574 5144-5157/? I/art: Starting a blocking GC HeapTrim




请问下是路径不对还是没权限?权限处我就写了一个
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

但是我路径写/sdcard/DCIM/test2.png也不能成功保存截图
...全文
944 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
讲究 2017-05-23
  • 打赏
  • 举报
回复
[android]使用 Uiautomator 来捕获图像/截图时出现异常 标签: Android 发布时间: 2013/12/21 22:23:14 本文来自: http://stackoverflow.com/questions/17810535/exception-while-using-uiautomator-to-capture-image-screenshot 注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接. 同时使用 UiautomatorTestcase 类来捕获截图的我主要活动我面对以下异常。 我使用的代码示例 public class MyActivityTest extends UiAutomatorTestCase { public UiautomatorAdaptxt() { // TODO Auto-generated constructor stub } @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); } public void testOpenMainActivity() throws UiObjectNotFoundException { -->Here I use the code to open my Activity<-- getUiDevice().takeScreenshot(storePath); } } 我与面对的例外是 java.lang.NoSuchMethodError: 在.testOpenMainActivity (MyActivityTest.java:31) 在 java.lang.reflect.Method.invokeNative (本机方法) 在 com.android.uiautomator.core.UiDevice.takeScreenshot com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java:124) 在 dalvik.system.NativeStart.main (本机方法) com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235) com.android.internal.os.RuntimeInit.nativeFinishInit (本机方法) com.android.commands.uiautomator.Launcher.main(Launcher.java:83) com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:76) com.android.uiautomator.testrunner.UiAutomatorTestRunner.run (UiAutomatorTestRunner.java:8 5) 解决方法 1: 你需要一个 Android 4.2 设备或稍后要采取这种方式的屏幕截图。 这是因为 takeScreenshot 目标被称为 Android 4.2 或 API 级别 17 中添加方法。 请参阅官方文档。 发生了什么是当您在您的本地计算机上编译时您编译针对 android 17 或稍后包含此方法,因此编译成功。然而,当部署于 android-16 目标 android 设备上时,此方法是失踪。当测试代码尝试调用此方法,它将引发 NoSuchMethodError 异常。 作为一种替代方法,您可以使用亚行的截图就像这样 adb shell screencap -p /data/local/tmp/screen-capture.png adb pull /data/local/tmp/screen-capture.png <localfile.png> adb shell rm /data/local/tmp/screen-capture.png

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧