在UncaughtExceptionHandler中截取屏幕截图(Take screenshot in UncaughtExceptionHandler)

编程入门 行业动态 更新时间:2024-10-23 06:26:45
在UncaughtExceptionHandler中截取屏幕截图(Take screenshot in UncaughtExceptionHandler)

我正在尝试实现一个系统,如果发生未捕获的异常,我的异常处理程序将截取该活动的屏幕截图,作为错误报告的一部分进行保存和发送。 这在Android中甚至可能吗? 我将活动传递给构造函数中的异常处理程序,但到目前为止我用于获取屏幕截图的每次尝试都返回null。

我尝试过以下方法:

尝试一:

private Bitmap screenshot() { View view = activity.getWindow().getDecorView(); //also tried getDecorView().getRootView() view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); view.setDrawingCacheEnabled(true); view.buildDrawingCache(true); Bitmap image = view.getDrawingCache(); Rect windowbounds = new Rect(); view.getWindowVisibleDisplayFrame(windowbounds); int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); Bitmap secondaryBitmap = Bitmap.createBitmap(image, 0, 0, width, height); view.destroyDrawingCache(); return secondaryBitmap; }

尝试二:

private Bitmap screenshot2() { View view = activity.getWindow().getDecorView(); //also tried getDecorView().getRootView() view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Bitmap viewBmp = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(viewBmp); view.draw(canvas); return viewBmp; }

在尝试#1中,view.getDrawingCache()返回null,并且在尝试#2 Bitmap.createBitmap返回null。

任何Android开发人员都知道如何在UncaughtExceptionHandler中截取屏幕截图?

I'm trying to implement a system whereby if an uncaught exception occurs, my exception handler will take a screenshot of the activity, to be saved and sent off as part of a bug report. Is this even possible in Android? I'm passing the activity to the exception handler in the constructor, but every attempt I've used so far to get the screenshot has returned null.

I've tried the following:

Attempt One:

private Bitmap screenshot() { View view = activity.getWindow().getDecorView(); //also tried getDecorView().getRootView() view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); view.setDrawingCacheEnabled(true); view.buildDrawingCache(true); Bitmap image = view.getDrawingCache(); Rect windowbounds = new Rect(); view.getWindowVisibleDisplayFrame(windowbounds); int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); Bitmap secondaryBitmap = Bitmap.createBitmap(image, 0, 0, width, height); view.destroyDrawingCache(); return secondaryBitmap; }

Attempt Two:

private Bitmap screenshot2() { View view = activity.getWindow().getDecorView(); //also tried getDecorView().getRootView() view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Bitmap viewBmp = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(viewBmp); view.draw(canvas); return viewBmp; }

In attempt #1 the view.getDrawingCache() returns null, and in attempt #2 Bitmap.createBitmap returns null.

Any Android developers have any idea on how to take a screenshot in the UncaughtExceptionHandler?

最满意答案

你不想要:

Bitmap viewBmp = Bitmap.createBitmap(view.getLayoutParams().width, view.getLayoutParams().height, Bitmap.Config.ARGB_8888);

LayoutParams通常不具有实际的宽度和高度。 通常它有负值,表示wrap_content或match_parent 。

相反,尝试:

Bitmap viewBmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

或类似的规定。 您需要容器的实际宽度和高度,而不是LayoutParams请求的宽度和高度。

You do not want:

Bitmap viewBmp = Bitmap.createBitmap(view.getLayoutParams().width, view.getLayoutParams().height, Bitmap.Config.ARGB_8888);

The LayoutParams does not normally have the actual width and height. Often it has negative values, indicating wrap_content or match_parent.

Instead, try:

Bitmap viewBmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

or something along those lines. You want the actual width and height of the container, not the width and height requested by its LayoutParams.

更多推荐

本文发布于:2023-04-29 13:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:截图   屏幕   UncaughtExceptionHandler   screenshot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!