While developing a camera app I\'ve encountered an exception that only happened when I switch to other app (onPause()
for my app).
01-15 17:22:1
I faced same issue, i fixed it by - Adding mCamera = null; in surfaceDestroyed(SurfaceHolder holder) method of Preview class.
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
and - Adding
camera = Camera.open();
camera.startPreview();
params = camera.getParameters();
preview.setCamera(camera);
in OnResume() method of my CameraActivity.
If you got:
Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewCallback(android.hardware.Camera$PreviewCallback)' on a null object reference
I agree with @ookami.kb - mCamera.setPreviewCallback(null);
is not enough, behind it also add this:
mCameraView.getHolder().removeCallback(mCameraView);
The docs clearly say that camera.release() releases all camera resources. After this call camera reference can not be used any more.
If you want to use camera again you have to acquire it via a open(int)
method.
It's all described in the camera docs.