[英]How to create screenshot in Robotium
我想知道如何在Robotium的AVD上截屏。 我已经读过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" />
但是我在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)
我手动创建目录以确保该目录存在。
我也试图在磁盘上截屏
String path = "/sdcard/SS";
solo.getConfig().screenshotSavePath = path;
solo.takeScreenshot("asd");
path = "C:/";
solo.getConfig().screenshotSavePath = path;
solo.takeScreenshot("asd");
但是错误仍然存在。 截屏有什么问题?
如果您已经创建了SS目录:
String path = Environment.getExternalStorageDirectory().getPath() + "/SS";
首先,按照@Seishin所说的那样创建路径:
String path = Environment.getExternalStorageDirectory().getPath() + "/SS";
然后,转到build.gradle
文件,并将targetSdkVersion
降级为22
。
高于22
,我的意思是: 23
或24
,需要定义Android Permissions Control,这与仅定义以下内容有所不同:
<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
降级。
希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.