自动化工具 安卓自动化基础设施探索:屏幕截图

甬力君 · 2019年08月21日 · 最后由 甬力君 回复于 2020年06月16日 · 1827 次阅读

之前发帖 https://testerhome.com/topics/12284 实现了 dex 和发射方式截图,发现在 9.0 上用不了,翻了安卓官方 aosp 源码才发现方法参数修改了。

sdk_level 大于27(安卓 9.0 pie)的已经修改了截图方法的参数,具体参考:

https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-vts-9.0_r10/core/java/android/view/SurfaceControl.java

sdk_level 大于 27 版本的具体使用方法

// rect是描述一个截取部分图的方块,我这里是截取全部,所以是(0, 0, 0, 0)
// screenWidth 屏幕宽度
// screenHeight 屏幕高度
// rotation 屏幕旋转角度,整数0|90|180|270
Rect rect = new Rect(0, 0, 0, 0);
int rotation = 0;
int screenWidth = 1080;
int screenHeight = 1920;
Bitmap bitmap = (Bitmap) Class.forName(surfaceClassName).getDeclaredMethod("screenshot",  new Class[]{Rect.class, Integer.TYPE, Integer.TYPE, Integer.TYPE })
                        .invoke(null, new Object[]{rect, screenWidth, screenHeight, rotation});

附送:各安卓版本截图相关接口参考:

// api 18 android-4.3_r0.9
public static Bitmap screenshot(int width, int height) {}

// api 19 android-4.4_r0.7
public static Bitmap screenshot(int width, int height) {}

// api 20 android-4.4w_r1
public static Bitmap screenshot(int width, int height) {}

// api 21 android-5.0.0_r1
public static Bitmap screenshot(int width, int height) {}

// api 22 android-5.1.0_r1 android-5.1.1_r4
public static Bitmap screenshot(int width, int height) {}

// api 23 android-6.0.0_r1 android-6.0.1_r74
public static Bitmap screenshot(int width, int height) {}

// api 24 android-7.0.0_r1
public static Bitmap screenshot(int width, int height) {}

// api 25 android-7.1.1_r4
public static Bitmap screenshot(int width, int height) {}

// api 26 android-8.0.0_r1 android-vts-8.0_r9
public static Bitmap screenshot(int width, int height) {}

// api 27 android-8.1.0_r60
public static Bitmap screenshot(int width, int height) {}

// api 28 android-9.0.0_r1 android-9.0.0_r30
public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 2 条回复 时间 点赞

你好,我的 Android9 手机用这个 api 截图 bitmap 返回 null,请问有什么原因造成呢?

tao lei 回复

API 接口不兼容导致的,需要自己加下接口适配

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册