Android相机2 api BufferQueue已被放弃

我正在testing新的Android camera2 API,我想控制每个相机的帧。 我所做的是创build一个ImageReader并设置分辨率和图像格式。

ImageReader imageReader = ImageReader.newInstance(1280,720,ImageFormat.YUV_420_888,1); imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); Log.i(MainActivity.LOG_TAG,"imageReader: "+System.currentTimeMillis()); image.close(); } },null); 

之后,我使用参数TEMPLATE_PREVIEW创build新的CaptureRequest ,并向他添加目标imageReader 。 对于cameraCaptureSession,我用这个请求创build新的setRepeatingRequest

  try { final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); builder.addTarget(imageReader.getSurface()); mCameraDevice.createCaptureSession( Arrays.asList(imageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { mSession = session; try { mSession.setRepeatingRequest(builder.build(),null,null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(CameraCaptureSession session) { } }, null ); } catch (CameraAccessException e) { e.printStackTrace(); } 

当我打开相机,并开始查看我有预览我得到exception。 但在例外之前,我得到了一些预览图像,之后我有例外

 10-30 16:00:32.850 1390-1894/.camera2tutorial E/BufferQueueProducer﹕ [unnamed-1390-1] dequeueBuffer: BufferQueue has been abandoned 10-30 16:00:32.850 1390-1894/.camera2tutorial E/Legacy-CameraDevice-JNI﹕ LegacyCameraDevice_nativeProduceFrame: Error while producing frame No such device (-19). 10-30 16:00:32.850 1390-1894/.camera2tutorial W/SurfaceTextureRenderer﹕ Surface abandoned, dropping frame. android.hardware.camera2.legacy.LegacyExceptionUtils$BufferQueueAbandonedException at android.hardware.camera2.legacy.LegacyExceptionUtils.throwOnError(LegacyExceptionUtils.java:64) at android.hardware.camera2.legacy.LegacyCameraDevice.produceFrame(LegacyCameraDevice.java:516) at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:699) at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:103) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:145) at android.os.HandlerThread.run(HandlerThread.java:61) 

我怎样才能解决这个问题??

我正在使用三星Galaxy S5和Android API 21

Solutions Collecting From Web of "Android相机2 api BufferQueue已被放弃"

确保你持有一个你创build的ImageReader的引用,可能在你定义了mSession的地方。

从ImageReader获得的Surface大致等价于一个弱指针 – 它不会阻止ImageReader被垃圾收集。 所以最有可能的(基于你的命名)ImageReader正在被破坏,然后发生抛弃错误。

我在我的应用程序中的活动之间切换时,也有同样的问题,这是在调用onSurfaceTextureDestroyed() ,只返回false,但我做了什么是我改变它

 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { Log.e(TAG, "onSurfaceTextureDestroyed"); if(cameraDevice != null){ closeCamera(); cameraDevice = null; } return false; } 

那为我工作。