java.lang.RuntimeException:release()之后调用的方法

时间:2013-12-27 12:20:40

标签: android camera

  1. 如果我在 surfaceDestroyed(....) <中未使用 mCamera.release(); / strong>然后 无法再次从其他活动启动CameraActivity [简而言之遗憾的应用已停止]错误, 甚至不发布相机 ,但如果我点击主页按钮 [来自CameraActivity ],然后再点击 启动我的应用 ,没有收到任何错误(简而言之,并且没有任何问题地打开CameraActivity)

  2. 如果我 使用 * mCamera.release(); *在 surfaceDestroyed(。 ...) 然后 能够再次从其他活动 启动CameraActivity, 再次发布相机 ,但是 当我点击“主页”按钮,然后再次启动我的应用时,感到很遗憾应用已停止 ....:

    < / LI>

    但我希望两件事情协同工作(首先, 从CameraActivity点击主页,再次从CameraActivity启动应用 - 没有任何错误)和(第二, 从其他活动中启动相机 - 没有任何错误)

    就像我写的那样,这两件事都适合我,但不是在一起......

    第33行是:

            Camera.Parameters parameters = mCamera.getParameters();
    

    完成日志:

    12-30 12:18:58.070: W/dalvikvm(14822): threadid=1: thread exiting with uncaught exception (group=0x41ef72a0)
    12-30 12:18:58.080: E/AndroidRuntime(14822): FATAL EXCEPTION: main
    12-30 12:18:58.080: E/AndroidRuntime(14822): java.lang.RuntimeException: Method called after release()
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.hardware.Camera.native_getParameters(Native Method)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.hardware.Camera.getParameters(Camera.java:1487)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at app.micheal.camr.PreviewSurface.surfaceCreated(PreviewSurface.java:33)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.SurfaceView.updateWindow(SurfaceView.java:609)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:235)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.View.dispatchWindowVisibilityChanged(View.java:7686)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1339)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4520)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer.doFrame(Choreographer.java:525)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.os.Handler.handleCallback(Handler.java:615)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.os.Handler.dispatchMessage(Handler.java:92)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.os.Looper.loop(Looper.java:137)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.app.ActivityThread.main(ActivityThread.java:4921)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at dalvik.system.NativeStart.main(Native Method)
    12-30 12:19:08.095: I/Process(14822): Sending signal. PID: 14822 SIG: 9
    

    PreviewSurface.java:

    public class PreviewSurface extends SurfaceView implements
    SurfaceHolder.Callback {
    
        public static final String LOG_TAG = "CameraPreview";
        private SurfaceHolder mSurfaceHolder;
    
        private Camera mCamera;
        Camera.Parameters parameters = null ;
    
        // Constructor that obtains context and camera
        @SuppressWarnings("deprecation")
        public PreviewSurface(Context context, Camera camera) {
            super(context);
            this.mCamera = camera;          
            this.mSurfaceHolder = this.getHolder();
            this.mSurfaceHolder.addCallback(this);
            this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            this.mSurfaceHolder.setFixedSize(100, 100);
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            try {       
                    parameters = mCamera.getParameters();
    
                    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) 
                    {
                         parameters.set("orientation", "portrait");
                         mCamera.setDisplayOrientation(90);
                         parameters.setRotation(90);
                         mCamera.setPreviewDisplay(surfaceHolder);
                         mCamera.startPreview();
                    }
                    else 
                    {
                         // This is an undocumented although widely known feature
                         parameters.set("orientation", "landscape");
                         // For Android 2.2 and above
                         mCamera.setDisplayOrientation(0);
                         // Uncomment for Android 2.0 and above
                         parameters.setRotation(0);
                    }
    
                    mCamera.setPreviewDisplay(surfaceHolder);
                    mCamera.startPreview();
    
                } catch (IOException e) {
                    // left blank for now
                }
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {         
            mCamera.stopPreview();
            mCamera.release();          
        }
    
    
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
                int width, int height) {
    
            try {       
                parameters = mCamera.getParameters();
                if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                     parameters.set("orientation", "portrait");
                     mCamera.setDisplayOrientation(90);
                     parameters.setRotation(90);
    
                }
                     else {
                          // This is an undocumented although widely known feature
                          parameters.set("orientation", "landscape");
                          // For Android 2.2 and above
                          mCamera.setDisplayOrientation(0);
                          // Uncomment for Android 2.0 and above
                          parameters.setRotation(0);
                }
                mCamera.setPreviewDisplay(surfaceHolder);
                mCamera.startPreview();
    
    
            } catch (IOException e) {
                // left blank for now
            }           
        }
    
    }
    

6 个答案:

答案 0 :(得分:22)

试试这个,用我的代码替换你的代码,我想这会对你有所帮助, 我没试过,但看起来会解决你的错误

      @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {     

        this.getHolder().removeCallback(this);
        mCamera.stopPreview();
        mCamera.release();
    }

尽快告诉我......

答案 1 :(得分:3)

很高兴,因为当您按下主页按钮时,您当前的活动将处于onStop模式&amp;当你再次回来时,它将从onRestart状态开始。不是来自PreviewSurface的构造函数,您可以在其中获取相机的保持力。所以,在没有人试图抓住同一台相机之前,没有人会这么做。

但是当你从另一个Activity开始呈现使用SurfaceView的Activity时,它将从构造函数&amp;将尝试抓住您之前的活动已经拥有的相同相机。

答案 2 :(得分:1)

我认为您的相机为空,看起来您忘了初始化相机

 mCamera = Camera.open();

答案 3 :(得分:1)

看看这个和

public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {       
     // here you should set  open camera . 

       mCamera= Camera.open();

        Camera.Parameters parameters = mCamera.getParameters();

        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) 
        {
             parameters.set("orientation", "portrait");
             mCamera.setDisplayOrientation(90);
             parameters.setRotation(90);
             mCamera.setPreviewDisplay(surfaceHolder);
             mCamera.startPreview();
        }
        else 
        {
             // This is an undocumented although widely known feature
             parameters.set("orientation", "landscape");
             // For Android 2.2 and above
             mCamera.setDisplayOrientation(0);
             // Uncomment for Android 2.0 and above
             parameters.setRotation(0);
        }
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();

    } catch (IOException e) {
        // left blank for now
    }
}

答案 4 :(得分:1)

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

@Override
    protected void onStop() {
        super.onStop();
        try {
            m_camera.stopPreview();
            m_camera.release();
            preview.removeView(m_CameraPreview);

            }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

m_CameraPreview是类的对象,如下所示: 公共类CameraSurfaceView扩展了SurfaceView实现的Callback

以下是onRestart方法:

    @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)

in OnPause stop the camera preview and release the camera
@Override
    public void onPause() {
        super.onPause();
        camera.stopPreview();
        camera.release();
        camera = null;
    }

in onResume initialize the camera

@Override
    public void onResume() {
        super.onResume();
        camera = Camera.open();
        surfaceView = (SurfaceView)view.findViewById(R.id.surfaceView);
        surfaceView.getHolder().addCallback(this);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
    }