Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top