Using ADB to capture the screen

JavaAndroidAdbScreenshot

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 - Java

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: https://stackoverflow.com/questions/13578416/read-binary-stdout-data-from-adb-shell

Solution 5 - Java

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

Solution 6 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2513924View Question on Stackoverflow
Solution 1 - JavaDiego PlentzView Answer on Stackoverflow
Solution 2 - JavaI'm a frog dragonView Answer on Stackoverflow
Solution 3 - JavaPremView Answer on Stackoverflow
Solution 4 - JavaJared RummlerView Answer on Stackoverflow
Solution 5 - JavabaitisjView Answer on Stackoverflow
Solution 6 - JavaalijandroView Answer on Stackoverflow