Skip to content

Instantly share code, notes, and snippets.

@danybony
Last active June 30, 2021 12:05
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danybony/d8bdb15445b4c3965f6196c6213fc545 to your computer and use it in GitHub Desktop.
Save danybony/d8bdb15445b4c3965f6196c6213fc545 to your computer and use it in GitHub Desktop.
Android screenshot to current dir
#!/bin/bash
while getopts y: flag
do
case "${flag}" in
y) size=${OPTARG};;
esac
done
DEVICES=`adb devices | grep -v devices | grep device | cut -f 1`
for device in $DEVICES; do
output="screen_$(echo $device)_$(date +%Y%m%d_%H%M%S).png"
echo "Capturing from $device"
adb -s $device shell screencap -p /sdcard/screen.png
adb -s $device pull /sdcard/screen.png $output
adb -s $device shell rm /sdcard/screen.png
if [ ! -z $size ]
then
sips -Z $size $output
fi
done
@danybony
Copy link
Author

danybony commented Mar 6, 2019

A small batch script that captures screenshots from all the connected Android devices and stores them in the current directory.

Optional parameter -y to specify the longest size to resize the grabbed screenshot. If not specified the original size will be kept.

@mayojava
Copy link

mayojava commented Mar 9, 2019

do you use this in some UI test? Or what is the use case. Thanks.

@pfmaggi
Copy link

pfmaggi commented Jun 30, 2021

Thanks for the script!
I've modified it a bit to suit my need (working with devices with multiple screens like the Samsung Z Fold):

#!/bin/bash

while getopts y: flag
do
    case "${flag}" in
        y) size=${OPTARG};;
    esac
done

DEVICES=`adb devices | grep -v devices | grep device | cut -f 1`
for device in $DEVICES; do
    DISPLAYS=`adb -s $device shell dumpsys SurfaceFlinger --display-id | cut -d" " -f 2`
    for display in $DISPLAYS; do
        echo "Capturing from $display on $device"
        output="screen_$(echo $device)_$(echo $display)_$(date +%Y%m%d_%H%M%S).png"
        adb -s $device exec-out screencap -p -d $display > $output

        if [ ! -z $size ]; then 
	        sips -Z $size $output
	    fi
    done
done

BTW: Using exec-out you can avoid to generate the file on the device and then copy/delete it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment