Search code examples
androidairadobescreenshotane

Adobe air ane for android - take screenshot returns black screen


I making ane which makes screenshot of application. But I always getting black rectangle.

public class TakeScreenshot implements FREFunction {
    @Override
    public FREObject call(FREContext context, FREObject[] args) {
        try {
            View view = context.getActivity().getWindow().getDecorView().getRootView();
            view.setDrawingCacheEnabled(true);
            Bitmap cachedBitmap = Bitmap.createBitmap(view.getDrawingCache());
            Bitmap bitmap = cachedBitmap.copy(cachedBitmap.getConfig(), false);

            FREBitmapData bmd = (FREBitmapData)args[0];
            bmd.acquire();
            bitmap.copyPixelsToBuffer(bmd.getBits());
            bmd.release();
            bmd.invalidateRect(0,0, bitmap.getWidth(), bitmap.getHeight());
            return bmd;

        } catch (FREWrongThreadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Guess, the problem is in taking correct View from FREContext. Have tried different ways to take View with same result:

View view = context.getActivity().getWindow().getDecorView();
View view = context.getActivity().findViewById(android.R.id.content);
View view = context.getActivity().findViewById(android.R.id.content).getRootView();

Solution

  • Unfortunately you cannot capture SurfaceView renders using this method which is how AIR renders its content.

    SurfaceView's will always render as black / empty content using the drawing cache. They contain 2 parts, a placeholder View and the render surface. The drawing cache method only captures the view part which is empty.

    On API 21 and higher you can use the MediaProjection class to perform a screencapture using a virtual display.

    This is the method we use in our Image ANE that can capture a screenshot:

    https://airnativeextensions.com/extension/com.distriqt.Image