title: Camera2 ImageReader hangs after a while with "Failed to release buffer" message
body: <p>I'm having a problem with android's camera2 API. 
  My end goal here is to have a byte array which I can edit using opencv, whilst displaying the preview to the user (e.g. an OCR with a preview). 
  I've create a capture request and added an ImageReader as a target. Then on the OnImageAvailableListener, i'm getting the image, transforming it to a bitmap and then display it on an ImageView (and rotating it). </p>
  
  <p>My problem is that after a few seconds, the preview stalls (after gradually slowing down) and in the log om getting the following error: <strong>E/BufferItemConsumer: [ImageReader-1225x1057f100m2-18869-0] Failed to release buffer: Unknown error -1 (1)</strong></p>
  
  <p>As you can see in my code, I have already tried closing the img after getting my byte[] from it.
  I've also tried clearing the buffer. 
  I've tried closing the ImageReader but that of course stopped me from getting any further images (throws an exception). </p>
  
  <p>Can anyone please help me understand what im doing wrong? I've been scouring google to no avail.</p>
  
  <p>This is my OnImageAvailableListener, do let me know if you need more of my code to assist:</p>
  
  <pre class="lang-java prettyprint-override"><code>private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
              = new ImageReader.OnImageAvailableListener() {
          @Override
          public void onImageAvailable(ImageReader reader) {
  
              Image img = reader.acquireLatestImage();
              final ImageView iv = findViewById(R.id.camPrev);
  
              try{
  
                  if (img==null) throw new NullPointerException("null img");
                  ByteBuffer buffer = img.getPlanes()[0].getBuffer();
                  byte[] data = new byte[buffer.remaining()];
                  buffer.get(data);
                  final Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          iv.setImageBitmap(b);
                          iv.setRotation(90);
                      }
                  });
              } catch (NullPointerException ex){
                  showToast("img is null");
              }finally {
                  if(img!=null)
                      img.close();
              }
          }
      };
  </code></pre>
  
  <p>Edit - adding cameraStateCallback</p>
  
  <pre><code>private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
          @Override
          public void onOpened(CameraDevice cameraDevice) {
              mCameraDevice = cameraDevice;
              showToast("Connected to camera!");
              createCameraPreviewSession();
          }
  
          @Override
          public void onDisconnected(CameraDevice cameraDevice) {
              closeCamera();
          }
  
          @Override
          public void onError(CameraDevice cameraDevice, int i) {
              closeCamera();
          }
      };
  
      private void closeCamera() {
          if (mCameraDevice != null) {
              mCameraDevice.close();
              mCameraDevice = null;
          }
  
      }
  </code></pre>