< Irrelevant.dev

Quick Capture Device Screenshots with adb

Published On: December 03, 2021 🌭?
Android CLI

When developing any UI, it's helpful to grab screenshots and/or video to share with your team for feedback, or just compare various designs. This can be surprisingly clunky when working on Android apps running on a physical device. You need to take the screenshot/video, and then get it to your development machine for sharing.

For the longest time I would grab a screenshot with the Power + Volume buttons, or for video, I would use something like AZ Recorder. Then I would email/slack the media to myself so I could get it onto my development machine. This worked, but interrupted my development flow enough I wanted a faster way.

Luckily, adb shell can help us out. We can capture and pull screenshots/videos of a device without leaving our terminal. Using screencap and screenrecord we can write a few utility scripts to make this smooth.

Grab a Screenshot named feature-a.png

android_grab_screenshot.sh <deviceId> feature-a.png

Capture and Pull a Video of your device called feature-a.mp4

android_record.sh <deviceId>
#Perform actions on device and CTRL+C to end recording
android_pull_recording.sh <deviceId> feature-a.mp4

Use adb devices to get the <deviceId> for your connected device.

Scripts

android_grab_screenshot.sh

#!/bin/bash

deviceId=$1
fileName=$2

if [ -z "$fileName" ]
then
        fileName="screenshot.png"
fi

if [ -z "$deviceId" ]
then
        echo "No device id provided. Grabbing without specific device Id"
        echo "Grabbing Screenshot. File will be called '$fileName'"
        adb shell /system/bin/screencap -p /sdcard/screenshot.png
        adb pull /sdcard/screenshot.png $fileName

else
        echo "Grabbing Screenshot. File will be called '$fileName'"
        adb -s $deviceId shell /system/bin/screencap -p /sdcard/screenshot.png
        adb -s $deviceId pull /sdcard/screenshot.png $fileName
fi

android_record.sh

#!/bin/bash

deviceId=$1

if [ -z "$deviceId" ]
then
        echo "No device id provided. Listing Available Devices."
        adb devices
        exit 1
fi

echo "Starting screen recording. Press CTRL+C to stop recording"
adb -s $deviceId shell screenrecord /sdcard/demo.mp4

android_pull_recording.sh

#!/bin/bash

deviceId=$1
fileName=$2

if [ -z "$deviceId" ]
then
        echo "No device id provided. Listing Available Devices."
        adb devices
        exit 1
fi

if [ -z "$fileName" ]
then
        fileName="demo.mp4"
fi

echo "Pulling latest recording. File will be called '${fileName}'"
adb -s $deviceId pull /sdcard/demo.mp4
mv demo.mp4 $fileName