A Program for Taking Screenshot for Android Device from Command Line

Android DDMS allows one to take screenshot easily by clicking a button. However, sometimes a developer may want to take screenshot from the command line in order to automate it. This post discusses how we can do this and provides a java program at the end.

0. How DDMS Capture Screenshot

DDMS capture screenshots through Android Debug Bridge (adb). It creates adb client, which communicates with adb server. Both adb client and server runs on the developer’s machine. DDMS sends screenshot command through adb client to adb server, the adb server then communicates with the daemon process runs on Android phone or emulator to get a screenshot.

Note that one needs to enable USB debugging in order for DDMS to capture screenshot.

1. Capture Screenshot from Command Line

Android source code contains a program to capture screenshot, but one needs to compile by oneself.

The code uses similar way DDMS uses to capture screenshot. First, it initiates a Android Debug Bridge instance by calling the code below,

AndroidDebugBridge.init(false); //initialize the library

AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();

The debug bridge will get a list of devices available. We can then query the list of devices by calling the code below,

IDevice[] devices = bridge.getDevices();

If there’s only one device available, we can proceed to screen capture by calling,

RawImage rawImage = device.getScreenshot();

If there’re multiple devices, one can select a device based on the serial number of the device. device.getSerialNumber() returns a string contains the serial number for the device.


One can download the code from here or my github here. If you only want to executable, you can get it here. Note that you’ll need to rename the file from screenshot.jar.zip to screenshot.jar.

Below is a screenshot of a sample execution and the phone screen captured.

Figure 1. Execution of screenshot.jar

Figure 2. Screenshot Captured

1. Android debug bridge: http://developer.android.com/guide/developing/tools/adb.html