[英]How to create screenshot in Robotium
I'd like to know how can I take screenshot on AVD in Robotium. 我想知道如何在Robotium的AVD上截屏。 I have read that 1st I have to have permissions but I've already done this. 我已经读过1st我必须具有权限,但是我已经做到了。
<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" />
But still I have error on Android Monitor 但是我在Android Monitor上还是有错误
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)
I created manually directory to be sure that dir exist. 我手动创建目录以确保该目录存在。
Also I was trying to take screenshot on my disk 我也试图在磁盘上截屏
String path = "/sdcard/SS";
solo.getConfig().screenshotSavePath = path;
solo.takeScreenshot("asd");
path = "C:/";
solo.getConfig().screenshotSavePath = path;
solo.takeScreenshot("asd");
but error still exist. 但是错误仍然存在。 What's wrong with taking screenshot? 截屏有什么问题?
如果您已经创建了SS目录:
String path = Environment.getExternalStorageDirectory().getPath() + "/SS";
First of all, create your path, as @Seishin said: 首先,按照@Seishin所说的那样创建路径:
String path = Environment.getExternalStorageDirectory().getPath() + "/SS";
Then, go to your build.gradle
file and downgrade targetSdkVersion
to 22
. 然后,转到build.gradle
文件,并将targetSdkVersion
降级为22
。
Higher than 22
, I mean: 23
or 24
, requires to define Android Permissions Control, which is a bit different than defining only: 高于22
,我的意思是: 23
或24
,需要定义Android Permissions Control,这与仅定义以下内容有所不同:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Read: https://developer.android.com/training/permissions/index.html 阅读: https : //developer.android.com/training/permissions/index.html
These kind of permissions (runtime permissions) work on Android 6.0 and higher version and the needed code looks like: 这些权限(运行时权限)适用于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.
}
}
To avoid it, just downgrade your targetSdkVersion
. 为了避免这种情况,只需将targetSdkVersion
降级。
Hope it will help 希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.