如何通过代码而不是程序来截取手机屏幕的选定区域的截图?


当前回答

private void captureScreen() {
    View v = getWindow().getDecorView().getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    try {
        FileOutputStream fos = new FileOutputStream(new File(Environment
                .getExternalStorageDirectory().toString(), "SCREEN"
                + System.currentTimeMillis() + ".png"));
        bmp.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在清单中添加权限

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

对于支持棉花糖或以上版本,请在onCreate方法中添加以下代码

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},00);

其他回答

作为参考,捕获屏幕(而不仅仅是应用程序活动)的一种方法是捕获帧缓冲区(device /dev/graphics/fb0)。要做到这一点,你必须拥有根权限,或者你的应用程序必须是一个具有签名权限的应用程序(“只有当请求应用程序与声明该权限的应用程序使用相同的证书进行签名时,系统才授予该权限”)-这是不太可能的,除非你编译了自己的ROM。

我测试过的几个设备上的每个framebuffer捕获都只包含一个截图。人们报告它包含更多,我猜这取决于框架/显示尺寸。

我尝试连续读取framebuffer,但它似乎返回了固定数量的读取字节。在我的例子中,这是(3 410 432)字节,这足以存储854*480 RGBA(3 279 360字节)的显示帧。是的,帧,二进制,从fb0输出是RGBA在我的设备。这很可能取决于不同的设备。这对你解码它很重要=)

在我的设备/dev/graphics/fb0权限是这样的,只有root和组图形的用户可以读取fb0。

图形是一个受限制的组,所以您可能只能使用su命令在根机中访问fb0。

Android应用程序的用户id (uid) = app_##,组id (guid) = app_##。

adb shell有uid = shell和guid = shell,它们拥有比app更多的权限。你可以在/system/permissions/platform.xml中查看这些权限

这意味着你将能够在没有根的adb shell中读取fb0,但如果没有根,你将无法在应用中读取它。

同样,在AndroidManifest.xml上给予READ_FRAME_BUFFER和/或ACCESS_SURFACE_FLINGER权限对常规应用没有任何作用,因为这些只对“签名”应用有效。

也检查这个关闭线程的更多细节。

private void captureScreen() {
    View v = getWindow().getDecorView().getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    try {
        FileOutputStream fos = new FileOutputStream(new File(Environment
                .getExternalStorageDirectory().toString(), "SCREEN"
                + System.currentTimeMillis() + ".png"));
        bmp.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在清单中添加权限

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

对于支持棉花糖或以上版本,请在onCreate方法中添加以下代码

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},00);

对于整页滚动截图

如果你想要捕捉一个完整的视图截图(其中包含一个滚动视图左右),那么在这个库中进行检查

https://github.com/peter1492/LongScreenshot

你所要做的就是导入Gradel,并创建一个bigscreen的对象

长截图=新BigScreenshot(这个,x, y);

当自动滚动屏幕视图组并在最后组装在一起时,将接收回调的屏幕截图位图。

get截图(Bitmap) {}

哪些可以保存到画廊或任何必要的用途,他们之后

注:仅适用于root phone

编程方式:adb shell /system/bin/screencap -p /sdcard/img.png

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();    

然后将img.png读取为位图,并按照您的意愿使用。

在android中截取视图的截图。

public static Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
        v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
}