[英]How to include screenshot function without rooting phone?
我是android函数和库功能的新手,所以我想问是否还有其他方法可以在我的应用程序中包含屏幕快照功能而无需扎根手机?
我在这里阅读的几乎所有文章仅会导致手机生根以应用屏幕快照功能。 但是,由于我的手机没有扎根,因此我在这些文章中添加了一个代码答案,并仅返回了黑色图像。 我猜。
还有其他方法还是我现在应该开始生根手机?
您不需要库或root即可获取自己应用程序的屏幕快照,因为该应用程序可以访问其所有自己的View
。 我们只需要获取Activity
的DecorView
,然后通过Canvas
将其绘制DecorView
Bitmap
。 下面的方法包括一个boolean
cropStatusBar
参数,以适应沉浸式模式捕获。
public static Bitmap getActivityScreenshot(Activity activity, boolean cropStatusBar) {
int statusBarHeight = 0;
if (cropStatusBar) {
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
statusBarHeight = activity.getResources().getDimensionPixelSize(resId);
}
View decor = activity.getWindow().getDecorView();
Bitmap result = Bitmap.createBitmap(decor.getWidth(),
decor.getHeight() - statusBarHeight,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
decor.setDrawingCacheEnabled(true);
Bitmap bmp = decor.getDrawingCache();
Rect src = new Rect(0, statusBarHeight, bmp.getWidth(), bmp.getHeight());
Rect dst = new Rect(0, 0, result.getWidth(), result.getHeight());
c.drawBitmap(bmp, src, dst, null);
decor.setDrawingCacheEnabled(false);
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.