Take screenshot of Android device using adb and save to filesystem

adb shell screencap -p | sed 's/\r$//' > FILENAME.PNG

0
By: alikhalil
2015-10-07 09:42:36
sed

These Might Interest You

What Others Think

Hmm... I don't like this. png is a binary format sed works linewise on text files . What are you trying to filter out of screencap's output?
flatcap · 137 weeks and 2 days ago
The ADB shell tty converts All "\n" to "\r\n", so sed works very well here because \r$ is the same as \r\n. Sure if you want to do binary replacements sed isn't the easiest, but this is pretty easy. Just look at the header of a png file: 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d adb shell outputs this: 89 50 4e 47 0d 0d 0a 1a 0d 0a 00 00 00 0d ^^^^^^ ^^^^^ Lets hope this formating works.. :-)
miniker84 · 136 weeks and 5 days ago
I see. Yuck :-) . Unfortunately, because png's binary it might legitimately contain the sequence 0D 0A. That will then be wrongly replaced with 0A. . The only safe solutions are the two parter: save on the device, then copy off: adb shell screencap -p /sdcard/screen.png adb pull /sdcard/screen.png . Or text encode the binary on the device, then decode locally. (or base64, etc). adb shell "screencap -p | uuencode" | uudecode >screencap.png
flatcap · 136 weeks and 4 days ago
Concrete example (using the google homepage image): curl -s https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png | sed 's/\r$//' > g.png file g.png g.png: data
flatcap · 136 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: