如何覆盖ScreenCaptureProcessor路径?

时间:2018-03-20 07:46:12

标签: android screenshot

嗨我实施截图到espresso测试 我成功了。

private void captureScreenshot(String name) {
ScreenCapture capture = Screenshot.capture();
    capture.setFormat(Bitmap.CompressFormat.PNG);
    capture.setName(name);
    try {
        capture.process();
    } catch (IOException ex) {
        throw new IllegalStateException(ex);

问题是我无法更改保存屏幕截图文件的路径   它是在BasicScreenCaptureProcessor类中设置的,但我不清楚如何覆盖路径或使用不同的路径创建自己的ScreencaptureProcessor。

这是android文档:

https://developer.android.com/reference/android/support/test/runner/screenshot/BasicScreenCaptureProcessor.html#mDefaultScreenshotPath

1 个答案:

答案 0 :(得分:0)

可以通过扩展BasicScreenCaptureProcessor来覆盖定义图像位置和其他细节的变量:

public class CustomScreenCaptureProcessor extends BasicScreenCaptureProcessor {

    public CustomScreenCaptureProcessor() {
        mTag = "CustomScreenCaptureProcessor";
        mFileNameDelimiter = "-";
        mDefaultFilenamePrefix = "screenshot";
        mDefaultScreenshotPath = InstrumentationRegistry.getTargetContext().getExternalFilesDir(null);
    }
}

最后,不要在process()上调用screenCapture,而是将其传递给CustomScreenCaptureProcessor实例,以便对其进行处理:

private CustomScreenCaptureProcessor processor = new CustomScreenCaptureProcessor();

private void captureScreenshot(String name) {
    ScreenCapture capture = Screenshot.capture();
    capture.setFormat(Bitmap.CompressFormat.PNG);
    capture.setName(name);
    try {
        processor.process(capture);
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}