Using ADB to capture the screen

162,957

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

  1. 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"
    }
    
  2. Run source ~/.bash_profile or source ~/.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:

https://github.com/baitisj/android_screen_mirror

Share:
162,957
user2513924
Author by

user2513924

I like programming

Updated on October 24, 2020

Comments

  • user2513924
    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
    user2513924 over 9 years
    Thanks 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
    user2513924 over 9 years
    I 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
    xdevs23 about 8 years
    You might use the stdout to transfer the file directly to the pc
  • Sumit
    Sumit almost 8 years
    This works in Linux too.
  • Vic Torious
    Vic Torious over 7 years
    I get "The file "screen.png" could not be opened." when I try to open it.
  • Alex P.
    Alex P. over 7 years
    no need to use stty. use adb exec-out instead. see stackoverflow.com/a/31401447/1778421
  • baitisj
    baitisj over 7 years
    Unfortunately, the version of adb that I have either does not properly support exec-out, or the option isn't properly supported under FreeBSD. The script that I posted does include comments indicating that exec-out is a better choice if it is supported in your environment.
  • cjones26
    cjones26 about 7 years
    Thanks Jared--unfortunately Diego's answer did not work for me, but yours did.
  • M. A.
    M. A. about 7 years
    What about Windows OS?
  • Dhiraj Gupta
    Dhiraj Gupta about 7 years
    I get an error: "sed: RE error: illegal byte sequence" when I try this.
  • Vineesh TP
    Vineesh TP about 7 years
    It says an error WorkingDirectoty: null Environment: null
  • Karu
    Karu almost 7 years
    This just moves a file around... doesn't actually take a screenshot?
  • Karu
    Karu almost 7 years
    This 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
    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
    Joshua Pinter almost 7 years
    Wonderful. Thanks.
  • Joshua Pinter
    Joshua Pinter almost 7 years
    Btw, 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 with adb_screenshot in the terminal and it will save a .png file with the current timestamp as the filename. The -d option in the adb 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
    DEEPANKUR SADANA over 6 years
    thanks @dragonwhospitsfire !
  • ramon
    ramon over 6 years
    For macOS: adb exec-out screencap -p > test.png
  • khateeb
    khateeb over 6 years
    I am getting PNG file corrupted by ASCII conversion when opening the PNG file.
  • Ninja
    Ninja almost 6 years
    For macOS 10.13.1 .adb exec-out screencap -p > screen.png got error. And @JoshuaPinter works!
  • webo80
    webo80 almost 6 years
    it works on Windows 10
  • muratoner
    muratoner over 5 years
    Awesome, this method very fast than adb push and adb pull methods.
  • Stéphane Gourichon
    Stéphane Gourichon over 4 years
    This 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
    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
    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
    Velda about 4 years
    Any reason it should not work on the other platform, than mentioned ones? It works, naturally, on Windows.
  • Display Name
    Display Name about 3 years
    this doesn't work. results in invalid file.
  • Sudhir Singh Khanger
    Sudhir Singh Khanger about 3 years
    What is the difference between exec-out screencap -p and shell screencap -p?
  • d.j.sheldrick
    d.j.sheldrick about 3 years
    For macOS copy-to-clipboard you can this tool that wraps adb npx android-capture image --copy
  • Joshua Pinter
    Joshua Pinter almost 3 years
    Figured 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
    Lachlan Arthur over 2 years
    For 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
    golimar almost 2 years
    Is 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 example echo hello > test.txt does create a 6 byte file