キャッチされていない例外が発生した場合、例外ハンドラーがアクティビティのスクリーンショットを撮り、保存してバグレポートの一部として送信するシステムを実装しようとしています。これはAndroidでも可能ですか?コンストラクターの例外ハンドラーにアクティビティを渡していますが、これまでスクリーンショットを取得するために使用したすべての試行で null が返されました。
私は次のことを試しました:
試行 1:
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;
}
試行 2:
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 でスクリーンショットを撮る方法を知っていますか?