note
note copied to clipboard
Android 程序中截屏
/**
* 获取屏幕截图
*
* @return 截图路径
*/
public static String screenCap() {
try {
Process sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
os.write("/system/bin/screencap -p /sdcard/screen.png".getBytes());
os.flush();
os.close();
sh.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return screenName;
}
使用系统命令 /system/bin/screencap -p filename
进行截屏并保存到filename。以上代码是在android中启动系统进程运行命令完成截屏操作。
另外还有一个命令 /system/bin/screenshot
, 这个命令保存的图片会错位,不知道什么原因。