97

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
  • 1,431
  • 1
  • 11
  • 22
  • Why is 3s slow for you? What is your intention to do with this? – dragi Jan 04 '15 at 15:06
  • I intended to get a live feed of the screen onto my pc.. Is this a stupid way of doing this? `adb shell screenrecord` was an option but it wouldn't be able to be accessed during the recording. – user2513924 Jan 04 '15 at 15:08
  • Do you need a live feed of only one application (that you develop), or it should work for anything that is running on that device? – dragi Jan 04 '15 at 15:10
  • It should work for anything. – user2513924 Jan 04 '15 at 15:28
  • Then screenrecord should be your solution. Did you check this http://developer.android.com/tools/help/adb.html#screenrecord ? I have used Android Studio to record and I guess it uses the same thing. I was able to use the device while recording the screen. What device do you use for this and what OS version is there? – dragi Jan 04 '15 at 15:32
  • This has been covered before. You may want to look into retutning the data through an adb forward. – Chris Stratton Jan 04 '15 at 15:34
  • helleye: I mean I wouldn't be able to grab the video file while it is being recorded as it blocks until you Ctrl-C or the time limit is hit. – user2513924 Jan 04 '15 at 15:36
  • Chris: It's the actual screencap function that takes time, not the `adb pull` as that completes in roughly 0.5s – user2513924 Jan 04 '15 at 15:38
  • Right, I forgot about that, sorry. Have you seen this answer http://stackoverflow.com/a/25859066/2160877 ? BTW, you don't just need to show it on a bigger screen, right? Because if that was the case you could use a monitor or TV as a secondary output screen (like HDMI). – dragi Jan 04 '15 at 15:46
  • You should update your question with the details you have shared in the comments (and add more if necessary) so that others(that don't read the comments here) will have a better understanding what you are asking for. – dragi Jan 04 '15 at 16:00
  • Yeah okay, also I want to do more than just see the screen. I want to be able to use the mouse to tap the screen as well but I have that figured out (`adb shell tap x y`) Also that answer from that link: I can't pull from /dev/graphics/fb0 because it says that it is a special file (folder). It might have something to do with the phone being a Samsung device but it is out of the option. – user2513924 Jan 04 '15 at 16:25

6 Answers6

228

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
Filip Czaplicki
  • 186
  • 3
  • 11
Diego Plentz
  • 5,527
  • 2
  • 26
  • 31
  • 3
    This works in Linux too. – Sumit Aug 02 '16 at 08:31
  • 25
    I get "The file "screen.png" could not be opened." when I try to open it. – Vic Torious Nov 09 '16 at 15:25
  • What about Windows OS? – M. A. Mar 08 '17 at 13:16
  • 1
    Wonderful. Thanks. – Joshua Pinter Jul 20 '17 at 20:49
  • 10
    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. – Joshua Pinter Jul 22 '17 at 00:39
  • 6
    For macOS: `adb exec-out screencap -p > test.png` – ramon Nov 18 '17 at 12:53
  • 1
    For macOS 10.13.1 .`adb exec-out screencap -p > screen.png` got error. And @JoshuaPinter works! – Ninja Jun 07 '18 at 08:01
  • 5
    it works on Windows 10 – webo80 Jun 18 '18 at 11:05
  • 1
    Awesome, this method very fast than adb push and adb pull methods. – muratoner Jan 02 '19 at 12:05
  • 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` – Stéphane Gourichon Jan 13 '20 at 12:49
  • 1
    @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. – nevelis Feb 26 '20 at 00:51
  • @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. – Joshua Pinter Mar 05 '20 at 21:31
  • Any reason it should not work on the other platform, than mentioned ones? It works, naturally, on Windows. – Velda Apr 28 '20 at 10:49
  • this doesn't work. results in invalid file. – Display Name Feb 12 '21 at 17:03
  • What is the difference between `exec-out screencap -p` and `shell screencap -p`? – Sudhir Singh Khanger Mar 14 '21 at 03:27
  • For macOS copy-to-clipboard you can this tool that wraps adb `npx android-capture image --copy` – d.j.sheldrick Apr 07 '21 at 08:27
12

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

Community
  • 1
  • 1
I'm a frog dragon
  • 6,717
  • 5
  • 38
  • 43
9

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.

ThomasW
  • 16,079
  • 4
  • 73
  • 101
Prem
  • 4,665
  • 4
  • 28
  • 61
6

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?

Community
  • 1
  • 1
Jared Rummler
  • 35,743
  • 18
  • 127
  • 142
  • Thanks Jared--unfortunately Diego's answer did not work for me, but yours did. – cjones26 Mar 01 '17 at 15:14
  • 6
    I get an error: "sed: RE error: illegal byte sequence" when I try this. – Dhiraj Gupta Mar 16 '17 at 08:13
  • 2
    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. – Karu May 08 '17 at 23:47
  • I am getting `PNG file corrupted by ASCII conversion` when opening the PNG file. – khateeb Nov 24 '17 at 06:59
4

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

baitisj
  • 209
  • 4
  • 5
  • no need to use `stty`. use `adb exec-out` instead. see http://stackoverflow.com/a/31401447/1778421 – Alex P. Dec 07 '16 at 03:29
  • 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. – baitisj Dec 07 '16 at 18:54
1

Sorry to tell you screencap just a simple command, only accept few arguments, but none of them can save time for you, here is the -h help output.

$ adb shell screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
-h: this message
-p: save the file as a png.
-d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

Besides the command screencap, there is another command screenshot, I don't know why screenshot was removed from Android 5.0, but it's avaiable below Android 4.4, you can check the source from here. I didn't make my comparison which is faster between these two commands, but you can give your try in your real environment and make the final decision.

alijandro
  • 9,944
  • 2
  • 45
  • 61
  • 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 Jan 04 '15 at 15:30
  • You might use the stdout to transfer the file directly to the pc – xdevs23 Apr 17 '16 at 14:26