释放Exception Camera预览后调用的方法

时间:2012-11-05 15:33:58

标签: android camera

我有一个活动类(CameraActivity)正在使用我的CameraPreview类。在“OnResume”中启动相机和预览。在“OnPause”中,我正在发布相机资源。当应用程序启动时,一切都在“OnResume”内正常工作,但是当我通过意图启动另一个活动(例如在浏览器中打开URL)然后回到我的活动时,在“OnResume”内发生了异常,发起了CamerPreview类。请在下面找到代码:

// CameraActivity类

public void onResume(){
    super.onResume();
    Log.d("inside onResume, camera==="+mCamera, "inside onResume");
    try {
        if(mCamera==null)
        {

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


        autoFocusHandler = new Handler();
        mCamera = getCameraInstance();
        int rotation = this.getWindowManager().getDefaultDisplay().getRotation();


        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 3);
        scanner.setConfig(0, Config.Y_DENSITY, 3);

        mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);

        FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
        preview.addView(mPreview);



    }





} catch (Exception e) {
    // TODO Auto-generated catch block
    Log.e("onResume",Log.getStackTraceString(e));
}



public void onPause{
    try {
        super.onPause();
        if (mCamera != null) {
            previewing = false;
            mCamera.stopPreview();
                mCamera.setPreviewCallback(null);
                mCamera.release();
                mCamera = null;
                mPreview=null;


    }
} catch (Exception e) {
    // TODO Auto-generated catch block
    Log.e("releaseCamera",Log.getStackTraceString(e));
}
}



 public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e){
            Log.e("getCameraInstance",Log.getStackTraceString(e));

    }
    return c;
}

//以下是CameraPreview类:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    Camera mCamera;
    PreviewCallback previewCallback;
    AutoFocusCallback autoFocusCallback;
    private int rotation;
    public int getRotation() {
        return rotation;
    }

    public void setRotation(int rotation) {
        this.rotation = rotation;
    }

    public CameraPreview(Context context, Camera camera,
                         PreviewCallback previewCb,
                         AutoFocusCallback autoFocusCb) {
        super(context);
        mCamera = camera;
        previewCallback = previewCb;
        autoFocusCallback = autoFocusCb;

        /*
         * Set camera to continuous focus if supported, otherwise use
         * software auto-focus. Only works for API level >=9.
         */
        /*
        Camera.Parameters parameters = camera.getParameters();
        for (String f : parameters.getSupportedFocusModes()) {
            if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
                mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                autoFocusCallback = null;
                break;
            }
        }
        */

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);

        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }


    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            if(mCamera==null){
                mCamera=Camera.open();
            }
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.d("DBG", "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {

        // Camera preview released in activity
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        /*
         * If your preview can change or rotate, take care of those events here.
         * Make sure to stop the preview before resizing or reformatting it.
         */
        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

try{
            mCamera.setPreviewDisplay(mHolder);
            mCamera.setPreviewCallback(previewCallback);
            mCamera.startPreview();
            mCamera.autoFocus(autoFocusCallback);
        } catch (Exception e){
            Log.d("DBG", "Error starting camera preview: " + e.getMessage());
        }
    }

}

这是来自logCat:

11-05 10:14:34.585: E/AndroidRuntime(7864): FATAL EXCEPTION: main
11-05 10:14:34.585: E/AndroidRuntime(7864): java.lang.RuntimeException: Method called after release()
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.hardware.Camera.setPreviewDisplay(Native Method)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.hardware.Camera.setPreviewDisplay(Camera.java:393)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at com.intagleo.qraugmented.detection.camera.CameraPreview.surfaceCreated(CameraPreview.java:74)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.SurfaceView.updateWindow(SurfaceView.java:552)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:215)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.View.dispatchWindowVisibilityChanged(View.java:4027)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewRoot.performTraversals(ViewRoot.java:790)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.os.Looper.loop(Looper.java:130)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at android.app.ActivityThread.main(ActivityThread.java:3687)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at java.lang.reflect.Method.invoke(Method.java:507)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-05 10:14:34.585: E/AndroidRuntime(7864):     at dalvik.system.NativeStart.main(Native Method)

修改

我更新了“surfaceDestroyed”并根据建议添加了日志,但现在我在“onPause”中获得了异常 - > onSurfaceDestroyed。最初onPause正在执行。

1-通过方法“getCameraInstance”在活动类的“onResume”中创建相机实例,并将mCamera实例传递给CameraPreview类。我尝试更改它,以便仅在“onSurfaceCreated”上创建相机实例,并将mCamera实例分配回活动类,但它不起作用。我还注意到,通过调试,“CameraPreview”类的previewCallBack成员第一次有效,但第二次“CameraPreview”类的“previewCallBack”成员为空。

请注意,第一次调用“onResume”时,一切正常,但是当它在onPause之后第二次运行时,最初会发生异常,尽管onResume中的代码相同。

11-06 01:25:28.375: I/onResume(4332): INITIATED
// Workinf fine till now. Now opening another intent activity
11-06 01:26:23.500: I/onPause(4332): INITIATED
11-06 01:26:23.804: "OnSurfaceDestroyed": "Initiated"
11-06 01:26:23.945: E/AndroidRuntime(4332): FATAL EXCEPTION: main
11-06 01:26:23.945: E/AndroidRuntime(4332): java.lang.RuntimeException: Method called after release()
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.hardware.Camera.stopPreview(Native Method)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at com.intagleo.qraugmented.detection.camera.CameraPreview.surfaceDestroyed(CameraPreview.java:85)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:596)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.SurfaceView.updateWindow(SurfaceView.java:490)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:215)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.View.dispatchWindowVisibilityChanged(View.java:4027)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewRoot.performTraversals(ViewRoot.java:790)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.os.Looper.loop(Looper.java:130)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at android.app.ActivityThread.main(ActivityThread.java:3687)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at java.lang.reflect.Method.invokeNative(Native Method)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at java.lang.reflect.Method.invoke(Method.java:507)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-06 01:26:23.945: E/AndroidRuntime(4332):     at dalvik.system.NativeStart.main(Native Method)

6 个答案:

答案 0 :(得分:41)

您已将相机添加到FrameLayout,如下所示:

FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);

将调用surfaceCreated方法,因此将调用mCamera.setPreviewDisplay(holder);

当你创建/打开一个新相机时,FrameLayout仍然有你以前的相机,所以除了你的新相机之外还会调用它surfaceCreated

当你松开相机时(FrameLayout方法),你应该做的就是从onPause()移除你以前的相机:

preview.removeView(mPreview);

希望它有所帮助。

答案 1 :(得分:5)

丹是对的。另请参阅here

代码示例:

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

答案 2 :(得分:1)

修改

这是一个难以回答的问题,我无法100%确定现在导致您的例外的原因。我在下方添加了我的相机代码,希望这会有所帮助,我使用startCamera()stopCamera()中的onPauseonResume方法。我还是,只在我的CameraPreview中创建onCreate类的新实例,除非onResume,否则我不会在cameraView == null中重新实例化。有几件事我们采取不同的做法。希望下面的代码有所帮助,也许你可以使用它来让你的工作:

P.S。:一切都适合我。即进入其他活动等。我尚未测试的生命周期的唯一部分是onDestroy,但这是因为我的应用程序设计为在此周期的开始处开始。

<强> MainActivity:

boolean cameraReleased = false;

@Override
protected void onPause() {
    Log.i("onPause", "CALLED:: cameraReleased = " + cameraReleased);
    Log.i("onResume", "CALLED:: cameraView = " + cameraView.toString());
    if (cameraReleased == false) {
        image = null;
        imageResult.setImageBitmap(null);
        imageResult.setImageResource(0);
        cameraView.stopCamera();
        cameraReleased = true;
    }

    if (cameraView == null) {
        Log.i("onPause", "cameraView == null");
        cameraView = new JJCameraSurfaceView(getApplicationContext());
        imageResult = new ImageView(getApplicationContext());
    }
    super.onPause();
}

@Override
protected void onDestroy() {
    Log.e("onDestroy", "INITIATED");
    super.onDestroy();
}

@Override
protected void onResume() {
    Log.i("onResume", "CALLED:: cameraReleased = " + cameraReleased);
    Log.i("onResume", "CALLED:: cameraView = " + cameraView.toString());
    if (cameraReleased == true) {
        image = null;
        imageResult.setImageBitmap(null);
        imageResult.setImageResource(0);
        cameraView.startCamera();
    }

    if (cameraView == null) {
        Log.i("onResume", "cameraView == null");
        cameraView = new JJCameraPreview(getApplicationContext());
        imageResult = new ImageView(getApplicationContext());
    }

    super.onResume();
}

@Override
public void onBackPressed() {
    // If Statement used to get out of my camera view and back to my MainActivity - Same Class
    if (“Camera Preview or Image Result is displayed”) {
        cameraView.stopCamera();
        image = null;
        imageResult.setImageBitmap(null);
        imageResult.setImageResource(0);
        cameraView.startCamera();
        return;
    }

    Log.i("onBackPressed", "WAS PRESSED");
    super.onBackPressed();
}

<强> CameraPreview:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Log.w("surfaceChanged", "STARTED");
    if (camera != null) {
        Log.w("surfaceChanged", "camera = NOT NULL");
        Camera.Parameters cParams = camera.getParameters();
        cParams.setPreviewSize(width, height);
        cParams.setSceneMode(Parameters.SCENE_MODE_NIGHT);
        camera.setParameters(cParams);
        camera.startPreview();
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.w("surfaceCreated", "STARTED");
    if (camera == null) {
        camera = Camera.open(); 
    }
    try {
        camera.setPreviewDisplay(mHolder);
    } catch (Exception e) {
        Log.e("setPreviewDisplay", "FAILED: " + e.getMessage());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.w("CameraSurfaceDestroyed", "INITIATED");
    camera.stopPreview();
    camera.release();
    camera = null;
}

public void startCamera() {
    Log.w("startCamera", "CALLED");
    mHolder = getHolder();
    surfaceCreated(mHolder);
    camera.startPreview();
    mHolder.addCallback(this);
}

public void stopCamera() {
    mHolder = getHolder();
    mHolder.removeCallback(this);
    camera.stopPreview();
}

答案 3 :(得分:1)

我的代码遇到类似问题,因为Method called after release()错误导致关闭。

我在SetupCamera()中调用OnResume()方法检查了一个空camera对象,然后调用了camera.Open()

解决了我的问题是解决了空检查,只是在阅读完之后调用camera.Open()是否为空(我已在camera = null上设置了onPauseCamera Docs

我知道这在追踪我的问题方面并不明确,但它对我来说绝对有效!

答案 4 :(得分:0)

尝试下面的代码,而不是重写onPause()和onResume(),覆盖onStop()和onRestart()。在活动生命周期中,当活动不可见时调用onStop(),下一个生命周期方法调用将调用onRestart()。看看下面的代码。

@Override
    protected void onStop() {
        super.onStop();
        try {
            m_camera.stopPreview();
            m_camera.release();
            preview.removeView(m_CameraPreview);
/*
m_CameraPreview is the object of the class that looks like this :                                public class CameraSurfaceView extends SurfaceView implements Callback
*/
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        m_camera=getCameraInstance();//Initialize the camera in your own way
        m_CameraPreview = new CameraSurfaceView(this, m_camera);
       preview = (FrameLayout)findViewById(R.id.camera_preview);   
        preview.addView(this.m_CameraPreview);
/* 
*camera_preview is the id of the framelayout defined in xml file and preview is *the instance of FrameLayout. 
*/
    }

正如丹所说,帧布局将保持前一个相机实例,并且除了创建竞争条件的新对象之外,还将创建其'surfaceview回调。因此,您需要在onStop()中释放它并在onRestart()中重新初始化。 希望这会有所帮助。

答案 5 :(得分:0)

我得到同样的错误。下面的序列解决了我的问题。

getHolder().removeCallback(this);

中致电surfaceDestroyed()

致电

 private void releaseCameraAndPreview() {

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

        if(mPreview != null){
            mPreview.destroyDrawingCache();
            mPreview.mCamera = null;
        }

    }
onPause

中的

致电

  private boolean safeCameraOpenInView(View view) {
        boolean qOpened = false;
        releaseCameraAndPreview();
        mCamera = getCameraInstance();
        mCameraView = view;
        qOpened = (mCamera != null);

        if(qOpened == true){
            mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera,view);
            preview = (FrameLayout) view.findViewById(R.id.camera_preview);
            preview.addView(mPreview);
            mPreview.startCameraPreview();
        }
        return qOpened;
    }
onCreateView()中的