在开发app的过程中,我们经常要获取一张app截图,以供和产品,设计师或者测试工程师交流时使用,这时我们想到的,当然是adb了。打开adb的使用说明文档,你可以找到用来获取截图的命令是screencap,使用方法如下:
adb shell screencap /sdcard/screen.png
但是,你会发现你不能将获取到的截图保存在你的电脑上,聪明的你肯定想到下面这种做法:
adb shell screencap /sdcard/screen.png
adb pull /sdcard/screen.png ~/Desktop/
但是你发现完成这个工作需要敲两行命令,太麻烦了,于是你写了一个shell脚本screencap.sh:
#! /bin/sh
# take a screenshot
adb shell screencap /sdcard/screen.png
# pull it from your device to your computer
adb pull /sdcard/screen.png ~/Desktop/
# delete the screenshot on your device since you already have it on your computer
adb shell rm /sdcard/screen.png
这样,以后你就可以执行screencap.sh获取到截图了,是不是很方便?
然而,很快你会发现问题所在,每次获取到的截图名字都是screen.png,每次截图就会把上一次的截图覆盖掉,这时你会想到,在将截图从调试机器pull到你的电脑上时,指定一个文件名:
#! /bin/sh
extention=".png"
file_name=$(basename "$1" ".png")$extention
# take a screenshot
adb shell screencap /sdcard/$file_name
# pull it from your device to your computer
adb pull /sdcard/$file_name ~/Desktop/
# delete the screenshot on your device since you already have it on your computer
adb shell rm /sdcard/$file_name
或者使用时间作为文件名,如果你懒得想名字的话:
#! /bin/sh
file_name=$(date "+%Y%m%d%H%M%S")".png"
# take a screenshot
adb shell screencap /sdcard/$file_name
# pull it from your device to your computer
adb pull /sdcard/$file_name ~/Desktop/
# delete the screenshot on your device since you already have it on your computer
adb shell rm /sdcard/$file_name
最后,作为一个爱折腾的程序员,你发现你有时候想指定截图存放在电脑上的路径,于是有了以下这个终极版的脚本:
#! /bin/sh
if [ "$1" = "." ]; then
echo "Invalid argument ".", A file name or a path is required to save your screenshots if any argument is presented."
exit
fi
default_dest=~/"Desktop"
extention=".png"
if [ $# -eq 0 ]; then
dest=${default_dest}
file_name=$(date "+%Y%m%d%H%M%S")$extention
else
if [ -d "$1" ]; then
dest="$1"
file_name=$(date "+%Y%m%d%H%M%S")$extention
elif [ -d $(dirname "$1") ] && [ $(dirname "$1") != "." ]; then
dest=$(dirname "$1")
file_name=$(basename "$1" ".png")$extention
else
dest=${default_dest}
file_name=$(basename "$1" ".png")$extention
fi
fi
# take a screenshot
adb shell screencap /sdcard/$file_name
# pull it from your device to your computer
adb pull /sdcard/$file_name $dest/$file_name
# delete the screenshot on your device since you already have it on your computer
adb shell rm /sdcard/$file_name
现在,有了这个小脚本,你可以:
1.临时截一张图,连名字都懒得想,就放在桌面,发给同事后马上删除掉:
screencap.sh
2.给截图取一个名字,放在桌面:
screencap somefeature.png
3.截一张图,放在自定义目录里,保留着,说不定哪天还要打开看看:
screencap ~/Desktop/work/someapp/screenshot/somefeature.png
网友评论