如何在Robotium中创建屏幕截图

时间:2016-08-12 12:27:54

标签: android android-studio robotium

我想知道如何在Robotium中截取AVD屏幕截图。我已经读过第一个我必须拥有权限,但我已经完成了这个。

<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)

我创建了手动目录以确保dir存在。

我还试图在我的磁盘上截屏

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

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

但错误仍然存​​在。截图有什么问题?

2 个答案:

答案 0 :(得分:0)

如果您已经创建了SS目录:

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

答案 1 :(得分:0)

首先,创建你的路径,如@Seishin所说:

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

然后,转到您的build.gradle文件并将targetSdkVersion降级为22

高于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

希望它会有所帮助