为什么我的相机应用程序崩溃了?

时间:2012-04-20 19:32:25

标签: android camera release

API 15.当我在拍照后停止相机并转到主屏幕并重新打开我的应用程序并尝试拍摄另一张照片时,我的应用程序崩溃了,我收到此错误:

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;
    }
}

2 个答案:

答案 0 :(得分:0)

从堆栈跟踪中,看起来您还有一些TimerTask仍然计划执行使用相机。它会在您关闭相机后触发,从而出现错误。您需要cancel() Timer,并且还准备好在您取消之前可能正在进行最后TimerTask的事实。因此,任务需要在操作之前检查相机是否已关闭。

答案 1 :(得分:-1)

基本上,在调用它的发布方法并且操作系统已将其发布到其本机代码中之后,您尝试使用mCamera对象。

该对象仍然存在(因此您的mCamera == null计算结果为false),但是如果不创建新对象或调用再次抓取本机资源的方法,则无法再使用该对象。