Android taking a screenshot with MediaProjectionManager, OnImageAvailable doesn't start

1.3k views Asked by At

I'm working on a android project, where there is an overlay button that is on top of other apps as well and when this button is clicked a screenshot should be taken. The overlay button works, but the screenshot part doesn't. I don't know why but it seems that the OnImageAvailable isn't starting. Here is a seperate project where I only test the screenshot part:

public class MainActivity extends Activity {

private static final int REQUEST_CODE= 100;

private MediaProjectionManager mProjectionManager;
private MediaProjection mProjection;
private ImageReader mImageReader;
private Handler mHandler = new Handler(Looper.getMainLooper());
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // call for the projection manager
    mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

    imageView = (ImageView) findViewById(R.id.imageView);

    // start projection
    final Button startButton = (Button)findViewById(R.id.button);
    startButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startProjection();
        }
    });

    // start capture handling thread
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler();
            Looper.loop();
        }
    }.start();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {

        mProjection = mProjectionManager.getMediaProjection(resultCode, data);

        if (mProjection != null) {
            final DisplayMetrics metrics = getResources().getDisplayMetrics();
            final int density = metrics.densityDpi;
            final int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
            final Display display = getWindowManager().getDefaultDisplay();
            final Point size = new Point();
            display.getSize(size);
            final int width = size.x;
            final int height = size.y;

            mImageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2);
            mProjection.createVirtualDisplay("screencap", width, height, density, flags, mImageReader.getSurface(), null, mHandler);
            mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {

                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    Bitmap bitmap = null;

                    try {
                        image = mImageReader.acquireLatestImage();
                        if (image != null) {
                            final Image.Plane[] planes = image.getPlanes();

                            int width = image.getWidth();
                            int height = image.getHeight();
                            int pixelStride = planes[0].getPixelStride();
                            int rowStride = planes[0].getRowStride();
                            int rowPadding = rowStride - pixelStride * width;
                            byte[] newData = new byte[width*height*4];

                            int offset = 0;
                            bitmap = Bitmap.createBitmap(metrics, width, height, Bitmap.Config.ARGB_8888);
                            ByteBuffer buffer = planes[0].getBuffer();
                            for(int i = 0; i < height; ++i){
                                for(int j = 0; j < width; ++j){
                                    int pixel = 0;
                                    pixel |= (buffer.get(offset) & 0xff) << 16;     // R
                                    pixel |= (buffer.get(offset + 1) & 0xff) << 8;  // G
                                    pixel |= (buffer.get(offset + 2) & 0xff);       // B
                                    pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
                                    bitmap.setPixel(j, i, pixel);
                                    offset += pixelStride;
                                }
                                offset += rowPadding;
                            }

                            imageView.setImageBitmap(bitmap);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {

                        if (bitmap!=null)
                            bitmap.recycle();

                        if (image!=null)
                            image.close();

                    }
                }

            }, mHandler);
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}

private void startProjection() {
    startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}

}
1

There are 1 answers

0
emandt On

I think you should set a different FLAGS. You're telling the MediaProjection to display ITS OWN CONTENT ONLY but you're not drawing nothing on its surface. To do a screenshot you have to REPLICATE A DIFFERENT DISPLAY using "DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR" flag.