If you want to take screen capture of something happening on your phone screen and save it as image to be sent to someone or own reference for analysis, Android provides an utility/command “screencap” using which which can save screen to image.
“screencap” command can be used as below to take screenshot and save as image,
$ adb shell screencap -p /sdcard/my_screenshot.png
Above command saves png image of screenshot in your device/mobile’s /sdcard folder, which we need to download for viewing, use below adb pull command to download to local machine,
$ adb pull /sdcard/my_screenshot.png
/sdcard/my_screenshot.png: 1 file pulled. 11.3 MB/s (1316280 bytes in 0.111s)
Now, you have the image on your local PC, you can open it from command line using command eog as,
$ eog my_screenshot.png
The other command line options supported by screencap command are as below,
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.
Important : Reduce filesize of screenshot to save disk space and Internet Bandwidth
As we can see above the filetype of screenshot take is “png” and size of file is large as around 11MB, which is huge if we want to share large number of files or upload to somewhere. The solution to this is, we can resize and convert the PNG file to JPG using “convert” command as,
$ convert my_screenshot.png -resize 70% my_screenshot.jpg
Here, we have used 70% resize size, but you can change to as required. Now if we change size of “JPG” file, my_screenshot.jpg which will be much smaller that original PNG file and become easier to share without compromising much on quality.
If you want to prevent others to take screenshot of your application, check our another post, “How to prevent Screenshot / Screen Capture in Android JAVA & Kotlin”
If you want to take video of screen, refer post “How to Record Screen Video in android using adb ?”