您的位置:首页 > 运维架构 > Shell

Android 使用shell screencap / screenshot命令截屏

2014-10-14 00:00 141 查看
摘要: 你将了解如何通过shell命令进行截屏

在Android里可以通过使用shell命令来截屏,一般用于应用程序外截屏,需要root权限。

1)在PC上使用cmd命令截屏

adb shell screencap -p /sdcard/123.png

或者

adb shell screenshot /sdcard/123.png

以上命令的意思是截取当前屏幕,以文件名123.png保存到sdcard目录下。

2)在Android应用里面截屏

/**
*
* @param path 图片保存路径
*/
public void screenshot(String path){
Process process = null;
try{
process = Runtime.getRuntime().exec("su");
PrintStream outputStream = null;
try {
outputStream = new PrintStream(new BufferedOutputStream(process.getOutputStream(), 8192));
outputStream.println("screencap -p " + path);
outputStream.flush();
}catch(Exception e){
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
process.waitFor();
}catch(Exception e){
e.printStackTrace();
}finally {
if(process != null){
process.destroy();
}
}
}

要给AndroidManifest.xml加上权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

详细见:https://www.zybuluo.com/olunx/note/18021
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息