Yhzhtk / note

知识代码笔记
https://github.com/Yhzhtk/note/issues
MIT License
108 stars 11 forks source link

Android 程序中截屏 #2

Open Yhzhtk opened 11 years ago

Yhzhtk commented 11 years ago
 /**
 * 获取屏幕截图
 * 
 * @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;
}
Yhzhtk commented 11 years ago

使用系统命令 /system/bin/screencap -p filename 进行截屏并保存到filename。以上代码是在android中启动系统进程运行命令完成截屏操作。

另外还有一个命令 /system/bin/screenshot, 这个命令保存的图片会错位,不知道什么原因。