title: Android ImageReader.acquireLatestImage returns invalid JPG
body: <p>I am using Android ImageReader class to receive Bitmaps from MediaProjection.createVirtualDisplay method. </p>
  
  <p>My code so far looks like this:</p>
  
  <pre><code>mProjection.createVirtualDisplay("test", width, height, density, flags, mImageReader.getSurface(), new VirtualDisplayCallback(), mHandler);
              mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                  @Override
                  public void onImageAvailable(ImageReader reader) {
                      Image image = null;
                      try {
                          image = mImageReader.acquireLatestImage();
                          final Image.Plane[] planes = image.getPlanes();
                          final ByteBuffer buffer = planes[0].getBuffer();
                          final byte[] data = new byte[buffer.capacity()];
                          buffer.get(data);
                          final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                          if (bitmap==null)
                              Log.e(TAG, "bitmap is null");
  
                      } catch (Exception e) {
                          if (image!=null)
                              image.close();
                      }
                  }
  
              }, mHandler);
  </code></pre>
  
  <p>The problem is that BitmapFactory cannot decode data[] back to Bitmap, i.e. BitmapFactory always returns null. The only messages I see from logcat come from <b>android_media_ImageReader.cpp</b> and go like this:</p>
  
  <pre><code>D/ImageReader_JNI(1432): ImageReader_imageSetup: Receiving JPEG in HAL_PIXEL_FORMAT_RGBA_8888 buffer.
  W/ImageReader_JNI(1432): Image_getJpegSize: No JPEG header detected, defaulting to size=width=3891200
  </code></pre>
  
  <p>Image object returned by acquireLatestImage is not null but not a valid JPEG either, I tried to check with the following test which fails:</p>
  
  <pre><code>if((buf [0] &amp; 0xFF) == 0xFF &amp;&amp; (buf[1] &amp; 0xFF) == 0xD8 &amp;&amp; (buf[2] &amp; 0xFF) == 0xFF &amp;&amp; (buf[3] &amp; 0xFF) == 0xE0)
      Log.e(TAG, "is JPG");
  else
      Log.e(TAG, "not a valid JPG");
  </code></pre>
  
  <p>The only think I am suspecting at the moment is that Android 5.0 emulator I am testing against cannot hanlde the API calls.</p>
  
  <p>Any ideas?</p>