API 15. When I stop my camera after taking a picture and go to the home screen and reopen my app and try to take another picture, my app crashes and I get this error:
04-20 12:04:38.437: E/AndroidRuntime(5150): FATAL EXCEPTION: Timer-2
04-20 12:04:38.437: E/AndroidRuntime(5150): java.lang.RuntimeException: Method called after release()
04-20 12:04:38.437: E/AndroidRuntime(5150): at android.hardware.Camera.native_takePicture(Native Method)
04-20 12:04:38.437: E/AndroidRuntime(5150): at android.hardware.Camera.takePicture(Camera.java:947)
04-20 12:04:38.437: E/AndroidRuntime(5150): at android.hardware.Camera.takePicture(Camera.java:892)
04-20 12:04:38.437: E/AndroidRuntime(5150): at com.prism.app.PrismActivity$5.run(PrismActivity.java:167)
04-20 12:04:38.437: E/AndroidRuntime(5150): at java.util.Timer$TimerImpl.run(Timer.java:284)
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
if (mCamera == null) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
// error setting preview of camera
}
} else {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release(); //need to take care of case when app is not closed completely still need to release
mCamera = null;
}
}
From the stack trace, it looks like you have some
TimerTaskthat is still scheduled to execute which uses the camera. It triggers after you close the camera, thus the error. You needcancel()theTimer, and also be prepared for the fact that the lastTimerTaskmay be in progress from before you cancel. So the task needs to check whether the camera is closed before it operates on it.