2

Using the MediaProjection API for screen capture, I create an ImageReader and feed use it as a point of access for captured screens as below:

mImageReader = ImageReader.newInstance(mWidth, mHeight, ImageFormat.JPEG, 2);

and

mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    Bitmap bitmap = null;
                    image = mImageReader.acquireLatestImage();
                    Image.Plane[] planes = image.getPlanes();
                    Buffer buffer = planes[0].getBuffer().rewind();
                    bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
                    bitmap.copyPixelsFromBuffer(buffer);
                }
            });

But the resulting image is always a blank transparent image and all pixels in the resulting bitmap are set to #00000000 :(

I've been stuck on this for a while and could really use some help so any advice is welcome. (Also I've already tried these posts but its all the same result - this, this and this)

Edit: I pass the image reader surface in this line:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int density = metrics.densityDpi;
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
mWidth = size.x;
mHeight = size.y;

mMediaProjection.createVirtualDisplay("screencap", mWidth, mHeight, density, flags, mImageReader.getSurface(), null, mHandler);
Community
  • 1
  • 1
SasukeIsCool
  • 227
  • 4
  • 11
  • Can you show the media projection setup, e.g. the place where you pass the ImageReader Surface in? – fadden Jul 29 '15 at 01:10
  • @fadden Ive edited the question to show this now – SasukeIsCool Jul 29 '15 at 08:27
  • 4
    Ah never mind. The code is actually alright. It works on devices but not in the emulator. Well thats 8 hours of my life i'm never getting back – SasukeIsCool Jul 30 '15 at 20:29
  • I also meet this issue. The code works on devices but not in the emulator. Do you know why and how to let it work in emulator? – alpha Dec 03 '15 at 06:35
  • 1
    Another question, I think your ImageReader uses PixelFormat.RGBA_8888 but not ImageFormat.JPEG? And the Bitmap uses Bitmap.Config.ARGB_8888, why you do not need to convert the format? – alpha Dec 03 '15 at 06:52

0 Answers0