Android-屏幕设备截屏

转载请注明:http://www.cnblogs.com/frank-zouxu/p/4158159.html 

今天,公司一个项目在进行中,遇到了屏幕取词的难题,对问题分步处理,首先得解决如何截取设备的屏幕,鉴于Android设备是基于Linux内核的,因此试着找到Android截图命令ScreenCap,在Android4.0的源码中找到:

 

其中ScreenCap是基于C++编写的,Screenshot是基于C编写的。这里使用ScreenCap.先进入手机的shell模式,进入命令行: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.

  

然后我们使用Java代码完成屏幕截取:
            Process sh = Runtime.getRuntime().exec("su", null,null);
                    OutputStream  os = sh.getOutputStream();
                    os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));  
            //在运行时使用shell命令完成截屏操作 os.flush(); os.close(); sh.waitFor();

 至此截屏文件(img.png)就被保存到了sd卡的根目录下了.时间仓促,整个过程叙述的不尽详细,只为记录今日所得。

 

  

 

posted on 2014-12-11 18:04  JasonZou_CN  阅读(1915)  评论(0编辑  收藏  举报

导航