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,
The debug bridge will get a list of devices available. We can then query the list of devices by calling the code below,
If there’s only one device available, we can proceed to screen capture by calling,
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.
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