如何在Robotium中创建屏幕截图

问题描述:

我想知道如何在Robotium的AVD上截屏。我已经读过,我必须有权限,但我已经做到了。如何在Robotium中创建屏幕截图

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

但我依然有错误在Android监视器

D/Robotium: Can't save the screenshot! Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test. 
W/System.err: java.io.FileNotFoundException: /sdcard/Screenshot/asd.jpg: open failed: EACCES (Permission denied) 

我手动创建的目录,确保目录存在。

我也试图采取截图我的磁盘

String path = "/sdcard/SS"; 
solo.getConfig().screenshotSavePath = path; 
solo.takeScreenshot("asd"); 

path = "C:/"; 
solo.getConfig().screenshotSavePath = path; 
solo.takeScreenshot("asd"); 

但错误依然存在的。采取截图有什么问题?

+0

[Android的,Robotium - 问题采取截图]的可能的复制(http://*.com/questions/8819542/android-robotium-issue-taking-screenshot) – piotrek1543

+0

检查上面的链接 – piotrek1543

如果您已经创建了SS DIR:

String path = Environment.getExternalStorageDirectory().getPath() + "/SS"; 
+0

它会帮助解决权限错误吗? –

+0

你不能像这样指出sdcard的根目录:“/ sdcard”。您必须通过系统的Environment.getExternalStorageDirectory()来获取存储目录,因为不同设备上的SD卡的安装点可能不同。 所以它可能可以帮助你解决这个权限错误。 – Seishin

+0

它仍然没有工作 - 同样的错误。这个代码的路径是/ storage/emulated/0,但它链接到/ sdcard/ –

首先,创建路径,@Seishin说:

String path = Environment.getExternalStorageDirectory().getPath() + "/SS"; 

然后,转到您的build.gradle文件和降级targetSdkVersion22

22更高

,我的意思是:2324,需要定义Android权限控制,这比只定义有点不同:

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

阅读:https://developer.android.com/training/permissions/index.html

这些类型的权限(运行时权限)适用于Android 6.0及更高版本,所需代码如下所示:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
       Manifest.permission.READ_CONTACTS) 
     != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Manifest.permission.READ_CONTACTS)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
       new String[]{Manifest.permission.READ_CONTACTS}, 
       MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

要避免它,只需降级您的targetSdkVersion

希望这将有助于

+0

我设置了路径,就像你说的,在我的build.gradle中只有'compileSdkVersion'和'buildToolsVersion',两者现在都在22版本,并且根本不工作。我看到我的AndroidManifest.xml很奇怪,因为'它说LauncherActivity无法解析。同样在''。无论如何,采取ss并不是高优先级,但它的工作很奇怪。 –

+0

你有哪些Android版本的设备? – piotrek1543

+0

它是6.0(23API),但我也试过5.1(22API) –