超简单实现Android 屏幕截屏(需要系统级权限)

1.思路 通过cmd 命令 :adb shell screencap -p /sdcard/screen.png 来实现

2.添加系统签名及权限
 

    android:sharedUserId="android.uid.system"<!-- 当然需要系统签名的支持 -->

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

3.创建 命令工具类:CmdUtil

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class CmdUtil {
    private static final String TAG = "CmdUtil";

    private static final String COMMAND_SH = "sh";
    private static final String COMMAND_EXIT = "exit\n";
    private static final String COMMAND_LINE_END = "\n";


    /**
     * 运行命令
     *
     * @param command 命令
     * @return 结果
     */
    public static Result execute(String command) {
        Log.i(TAG, "execute() command = " + command);
        Result result = new Result();

        if (TextUtils.isEmpty(command)) {
            Log.w(TAG, "WARNING: command should not be null or empty");
            return result;
        }

        Process process = null;
        DataOutputStream dos = null;

        try {
            process = Runtime.getRuntime().exec(COMMAND_SH);
            dos = new DataOutputStream(process.getOutputStream());
            dos.write(command.trim().getBytes());
            dos.writeBytes(COMMAND_LINE_END);
            dos.flush();
            dos.writeBytes(COMMAND_EXIT);
            dos.flush();
            result.code = process.waitFor();
            result.success = readBuffer(new BufferedReader(new InputStreamReader(process.getInputStream())));
            result.error = readBuffer(new BufferedReader(new InputStreamReader(process.getErrorStream())));
            Log.i(TAG, "result = " + result);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            Log.e(TAG, ioe.getMessage());
        } catch (InterruptedException ie) {
            ie.printStackTrace();
            Log.e(TAG, ie.getMessage());
        } finally {
            try {
                if (null != dos) {
                    dos.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
                Log.e(TAG, ioe.getMessage());
            }
            if (null != process) {
                process.destroy();
            }
        }
        return result;
    }

    @NonNull
    private static String readBuffer(BufferedReader bufferedReader) throws IOException {
        StringBuilder sb = new StringBuilder();
        String s;
        while ((s = bufferedReader.readLine()) != null) {
            sb.append(s);
        }
        return sb.toString();
    }


    /**
     * Command执行结果
     */
    public static final class Result {

        public static final int SUCCESS = 0;
        public static final int ERROR = -1;

        public int code = ERROR;
        String error;
        String success;

        @Override
        public String toString() {
            return "Result{" +
                "code=" + code +
                ", error='" + error + '\'' +
                ", success='" + success + '\'' +
                '}';
        }
    }
}

 4.简单实现

 CmdUtil.execute(" screencap -p /data/screen1.png");

5.导出查看即可(或者指定其他路径)

adb pull /data/screen1.png ./

另附上两个很好的截屏demo:
参考Google官方给的demo以及其他开发者写的Demo

https://github.com/googlesamples/android-ScreenCapture
https://github.com/VincentWYJ/CaptureScreen

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值