[英]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.