@Test
    public void saveViewBitmap() throws IOException {
        onView(withText("Hello World!")).perform(captureToBitmap(new ViewActions.BitmapReceiver() {
            @Override
            public void onBitmapCaptured(Bitmap bitmap) {
                try {
                    writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }));


    }
Captures and saves an image of the 'Hello world' view.
ui/espresso/ScreenshotSample/app/src/androidTest/java/com/example/android/testing/espresso/screenshotsample/ScreenshotJavaTest.java