Method called after release Exception Camera preview

前端 未结 7 1319

I have one activity class(CameraActivity) which is using my CameraPreview class. In \"OnResume\" the camera and preview are initiated. In \"OnPause\", i am releasing camera

相关标签:
7条回答
  • 2020-12-13 12:07

    Dan is right. Also see here.

    code sample:

    public class MainActivity extends Activity {
        private FrameLayout mFlCameraPreview;
        private Camera mCamera;
        private CameraPreview mCameraPreview;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mFlCameraPreview = (FrameLayout) findViewById(R.id.main_fl_camera_preview);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (mCamera == null) {
                mCamera = getCameraInstance();
            }
    
            if (mCameraPreview == null) {
                mCameraPreview = new CameraPreview(this, mCamera);
                mFlCameraPreview.addView(mCameraPreview);
            }
    
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            if (mCamera != null) {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
    
            if (mCameraPreview != null) {
                mFlCameraPreview.removeView(mCameraPreview);
                mCameraPreview = null;
            }
        }
    
        public static Camera getCameraInstance() {
            Camera camera = null;
            try {
                camera = Camera.open();
            } catch (Exception e) {
    
            }
            return camera;
        }
    }  
    
    0 讨论(0)
提交回复
热议问题