Using ADB to capture the screen
Solution 1
To save to a file on Windows, OSX and Linux
adb exec-out screencap -p > screen.png
To copy to clipboard on Linux use
adb exec-out screencap -p | xclip -t image/png
Solution 2
https://stackoverflow.com/a/37191719/75579 answer stopped working for me in Android 7 somehow. So I have to do it the manual way, so I want to share it.
How to install
-
Put this snippet of code in your
~/.bash_profile
or~/.profile
file:snap_screen() { if [ $# -eq 0 ] then name="screenshot.png" else name="$1.png" fi adb shell screencap -p /sdcard/$name adb pull /sdcard/$name adb shell rm /sdcard/$name curr_dir=pwd echo "save to `pwd`/$name" }
Run
source ~/.bash_profile
orsource ~/.profile
command,
How to use
Usage without specifying filename:
$ snap_screen
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/screenshot.png
Usage with a filename:
$ snap_screen mega_screen_capture
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/mega_screen_capture.png
Hope it helps!
** This will not work if multiple devices are plugged in
Solution 3
To start recording your device’s screen, run the following command:
adb shell screenrecord /sdcard/example.mp4
This command will start recording your device’s screen using the default settings and save the resulting video to a file at /sdcard/example.mp4 file on your device.
When you’re done recording, press Ctrl+C in the Command Prompt window to stop the screen recording. You can then find the screen recording file at the location you specified. Note that the screen recording is saved to your device’s internal storage, not to your computer.
The default settings are to use your device’s standard screen resolution, encode the video at a bitrate of 4Mbps, and set the maximum screen recording time to 180 seconds. For more information about the command-line options you can use, run the following command:
adb shell screenrecord --help
This works without rooting the device. Hope this helps.
Solution 4
You can read the binary from stdout instead of saving the png to the sdcard and then pulling it:
adb shell screencap -p | sed 's|\r$||' > screenshot.png
This should save a little time, but not much.
source: Read binary stdout data from adb shell?
Solution 5
Using some of the knowledge from this and a couple of other posts, I found the method that worked the best for me was to:
adb shell 'stty raw; screencap -p'
I have posted a very simple Python script on GitHub that essentially mirrors the screen of a device connected over ADB:
Comments
-
user2513924 over 3 years
I'm trying to get a screenshot of the phone screen as fast as possible. Currently, I am doing:
adb shell screencap -p /sdcard/screencap.png && adb pull /sdcard/screencap.png
However it is too slow and takes up to 3 seconds. Is there any better way to do this? I intend to use this function with an unrooted phone.
Also what are the different arguments I can use for screencap?
Thanks.
EDIT (extra information): I intend to use this method to be able to get a live feed of the screen onto my pc. The current method works however it is too slow. I can't use
adb shell screenrecord
because I won't be able to access the video file while it is being recorded. -
user2513924 over 9 yearsThanks for that. Turns out the screenshot function is the actual device screenshot function (where it can play a sound) whereas screencap just takes a screenshot silently. It doesn't look like it's any faster but thanks anyway.
-
user2513924 over 9 yearsI won't be able to get a live feed of the video unless the file is being recorded directly to my pc. Thanks anyway though.
-
xdevs23 about 8 yearsYou might use the stdout to transfer the file directly to the pc
-
Sumit almost 8 yearsThis works in Linux too.
-
Vic Torious over 7 yearsI get "The file "screen.png" could not be opened." when I try to open it.
-
Alex P. over 7 years
-
baitisj over 7 yearsUnfortunately, the version of
adb
that I have either does not properly supportexec-out
, or the option isn't properly supported under FreeBSD. The script that I posted does include comments indicating thatexec-out
is a better choice if it is supported in your environment. -
cjones26 about 7 yearsThanks Jared--unfortunately Diego's answer did not work for me, but yours did.
-
M. A. about 7 yearsWhat about Windows OS?
-
Dhiraj Gupta about 7 yearsI get an error: "sed: RE error: illegal byte sequence" when I try this.
-
Vineesh TP about 7 yearsIt says an error WorkingDirectoty: null Environment: null
-
Karu almost 7 yearsThis just moves a file around... doesn't actually take a screenshot?
-
Karu almost 7 yearsThis always worked for me, but I just had to change this to
sed 's|\r\r$||'
(to remove TWO carriage returns) because I started getting corrupt PNGs. Not sure what changed (New version of ADB? New version of cygwin?) but something did. -
I'm a frog dragon almost 7 years@Karu oh no, you're so right, I somehow left out the important line, I just fixed it!
-
Joshua Pinter almost 7 yearsWonderful. Thanks.
-
Joshua Pinter almost 7 yearsBtw, because I use this frequently for sharing with designers and for product tutorials, I created a bash alias in my
~/.bash_profile
that looks like this:alias adb_screenshot="adb -d shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > $(date +"%Y-%m-%d_%H-%M-%S").png"
. You call it withadb_screenshot
in the terminal and it will save a .png file with the current timestamp as the filename. The-d
option in theadb
command means it will default to physical device (not the emulator). You can remove that if you are using this for an emulator. -
DEEPANKUR SADANA over 6 yearsthanks @dragonwhospitsfire !
-
ramon over 6 yearsFor macOS:
adb exec-out screencap -p > test.png
-
khateeb over 6 yearsI am getting
PNG file corrupted by ASCII conversion
when opening the PNG file. -
Ninja almost 6 yearsFor macOS 10.13.1 .
adb exec-out screencap -p > screen.png
got error. And @JoshuaPinter works! -
webo80 almost 6 yearsit works on Windows 10
-
muratoner over 5 yearsAwesome, this method very fast than adb push and adb pull methods.
-
Stéphane Gourichon over 4 yearsThis is very nice, yet overwrites the same file again and again. This variant writes several files with names arranged to be in correct order whatever (sane) program you use to browse them:
adb exec-out screencap -p > android_screenshot_"$( date +%Yy%mm%dd_%Hh%Mm%Ss )".png
-
nevelis about 4 years@JoshuaPinter Your alias would be better as
alias adb_screenshot="adb -d exec-out screencap -p > $(date +"%Y-%m-%d_%H-%M-%S").png"
- that regex can corrupt the file if there is a valid 0x0D 0x0A sequence in the png data. -
Joshua Pinter about 4 years@nevelis You're right! Just tried my command and it resulted in a corrupt file but yours worked perfectly. Thanks! You should add it as an answer so people can find it. It's great.
-
Velda about 4 yearsAny reason it should not work on the other platform, than mentioned ones? It works, naturally, on Windows.
-
Display Name about 3 yearsthis doesn't work. results in invalid file.
-
Sudhir Singh Khanger about 3 yearsWhat is the difference between
exec-out screencap -p
andshell screencap -p
? -
d.j.sheldrick about 3 yearsFor macOS copy-to-clipboard you can this tool that wraps adb
npx android-capture image --copy
-
Joshua Pinter almost 3 yearsFigured I'd post an update here since my last post was well liked but is now outdated. Here is what we're using now, which takes a screenshot of all the devices attached (very useful for those working with multiple devices) and saves the screenshots with the serial number of each device and current timestamp. Enjoy.
adb devices -l | cut -d' ' -f1 | xargs -n1 -I{} sh -c "adb -s {} exec-out screencap -p > {}_$(date +'%Y-%m-%d_%H-%M-%S').png"
-
Lachlan Arthur over 2 yearsFor Windows users: Run the command in CMD, not PowerShell (or run
cmd
in PowerShell). PowerShell is corrupting the binary data when creating the file (it is interpreting the piped PNG image data as text). -
golimar almost 2 yearsIs this exclusively from adb? I tried from the actual phone (on 2 different devices) with a terminal app with
$ screencap test.png
and it generates a 0 byte file. The folder is writable, for exampleecho hello > test.txt
does create a 6 byte file