Android Screen Capture Using ADB

Objective: Take a screenshot of an Android device display using adb shell.

To take a screenshot of an Android device display, we will need to use the screencap utility. This does not require root access. Screenshots are saved in PNG format.

Connect your Android device to the computer with a USB cable. Make sure that USB debugging is enabled on the device and run adb using the following syntax.

$ adb shell screencap /sdcard/screen.png

The screenshot will be saved to /sdcard/screen.png. Note that if there’s already an existing file, it will be overwritten. If you would like to save the output file with another extension other than .png, then you will need to pass the -p option to screencap.

$ adb shell screencap -p /sdcard/screen.bin

To pull the file from your Android device to your computer, use the adb pull command.

$ adb pull /sdcard/screen.png

Once the file has been transferred to your computer, you can delete the file from your Android device.

$ adb rm /sdcard/screen.png

If you would like the screenshot file to be written directly to your computer, then we can tell screencap to write the output to stdout instead. But there’s a small problem. Apparently, when writing the screenshot data to stdout, adb shell converts 0x0a (linefeed) to 0x0d0a (carriage return, linefeed) – this will corrupt the output screenshot file.

The solution is to use sed to undo the conversion.

$ adb shell "screencap -p" | sed 's/\r$//' > screen.png

Another option is to use uuencode and uudecode.

$ adb shell "screencap -p | uuencode -" | uudecode > screen.png

Alternatively, you can also use base64 instead of uuencode/uudecode.

$ adb shell "screencap -p | base64" | sed 's/\r$//' | base64 -d > screen.png

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react, vuejs); plays_on(xbox, ps4); linux_desktop_user(true); }