我正在尝试实现一个系统,如果发生了一个未察觉的异常,我的异常处理程序将获取该活动的屏幕快照,并将其保存并作为bug报告的一部分发送出去。这在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。
任何安卓开发者对如何在UncaughtExceptionHandler中截图有任何想法吗?
发布于 2013-10-28 11:39:16
你不想:
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请求的宽度和高度。
https://stackoverflow.com/questions/19633725
复制相似问题