DeviceCapture


public final class DeviceCapture


Summary

Public methods

static final boolean

Returns false if calling takeScreenshot will fail.

static final @NonNull Bitmap

Captures an image of the device's screen into a Bitmap.

Public methods

canTakeScreenshot

@ExperimentalTestApi
public static final boolean canTakeScreenshot()

Returns false if calling takeScreenshot will fail.

Taking a screenshot requires UiAutomation and can only be called off of the main thread. If this method returns false then attempting to take a screenshot will fail. Note that taking a screenshot may still fail if this method returns true, for example if the call to UiAutomation fails.

takeScreenshot

@ExperimentalTestApi
public static final @NonNull Bitmap takeScreenshot()

Captures an image of the device's screen into a Bitmap.

This is essentially a wrapper for UIAutomation#takeScreenshot() that attempts to get a stable screenshot by forcing all the current application's root window views to redraw, and also handles cases where hardware renderer drawing is disabled.

This API is intended for use cases like debugging where an image of the entire screen is needed. For use cases where the image will be used for validation, its recommended to take a more isolated, targeted screenshot of a specific view or compose node. See androidx.test.core.view.captureToBitmap, androidx.test.espresso.screenshot.captureToBitmap and androidx.compose.ui.test.captureToImage.

This API does not support concurrent usage.

This API is currently experimental and subject to change or removal.

Returns
@NonNull Bitmap

a Bitmap that contains the image

Throws
kotlin.IllegalStateException

if called on the main thread. This is a limitation of connecting to UiAutomation, RuntimeException if UiAutomation fails to take the screenshot