1. 程式人生 > >Camera is being used after Camera.release() was called

Camera is being used after Camera.release() was called

 @Override
    protected void onDestroy() {
        super.onDestroy();
        if (camera!=null){
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    }

報錯原因 :

       手動呼叫上面onDestroy()的時候,PreviewCallback介面onPreviewFrame方法還在不停呼叫,具體呼叫頻率就是當前預覽的FrameRate,當onDestroy()執行完camera.release()時,onPreviewFrame再次被呼叫時就出現了該異常。  

解決辦法:

   if (camera!=null){
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }

在onDestroy()裡面呼叫camera.release()之前,先呼叫一次camera.setPreviewCallback(null); ,這樣在執行完camera.release(),因為指定的PreviewCallback為null,因而就不會再呼叫onPreviewFrame,進而也不會再引用到camera和呼叫到其任何方法。

注:

onPreviewFrame第二個引數引用了camera。

應根據自己的release時機呼叫setPreviewCallback(null),例子是在OnDestroy中呼叫。