java.lang.RuntimeException: Method called after release()

13,489

Solution 1

try this, replace your code with mine, i guess this will help you, i have not tried but looks it will resolve your errors

      @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {     

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

Let me know ASAP...

Solution 2

It was happning because when you press the Home button, your present Activity will be in onStop mode & when you will come back again it will start from onRestart state. Not from the constructor of PreviewSurface where you are acquiering a hold on the Camera. So, before relese nobody is trying to get a hold on the same Camera.

But when you start another Activity from you present Activity which is using a SurfaceView, that will start from the constructor & will try to get a hold on the same Camera which is already held by your previous Activity.

Solution 3

Instead of overriding onPause() and onResume(), override the onStop() and onRestart(). In the activity lifecycle, onStop() is called when the activity is not Visible and the next lifecycle method call would be to the onRestart(). Have a look at the code below.

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

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

m_CameraPreview is the object of the class that looks like this: public class CameraSurfaceView extends SurfaceView implements Callback

Following is the onRestart method:

    @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. 
*/
    }

The frame layout will have a hold to the previous camera instance and its' surfaceview callbacks will be created in addition to the new camera object, creating a race condition. Hence you would need to release it in onStop() and reinitialize in onRestart(). Hope this helps.

Solution 4

i think your mCamera is null,looks like you forgot to intialize camera

 mCamera = Camera.open();

Solution 5

look at this ans

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
    }
}
Share:
13,489
Sun
Author by

Sun

Updated on July 21, 2022

Comments

  • Sun
    Sun over 1 year
    1. If i am not using mCamera.release(); in surfaceDestroyed(....) then not able to launch CameraActivity again from another Activity [in short getting Unfortunately app has stopped] error, even not releasing Camera, but if i do tap on Home button [from CameraActivity], and then again launching my app, not getting any error (in short works fine, and opening CameraActivity without any problem)

    2. And if i am using *mCamera.release();* in surfaceDestroyed(....) then able to launch CameraActivity again from another Activity and releasing Camera as well, but when i do tap on Home button, and then again launching my app, getting Unfortunately app has stopped....:

    But i want both things working together (first, Tap on Home from CameraActivity, and again launch app from CameraActivity - without any error) and (second, launching camera from another activity - without any error)

    Like i wrote, both the things are working for me, but not together.....

    Line number 33 is:

            Camera.Parameters parameters = mCamera.getParameters();
    

    complete Log:

    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
            }           
        }
    
    }
    
  • praveen
    praveen over 10 years
    before calling your method surfaceCreated(SurfaceHolder surfaceHolder)
  • Sun
    Sun over 10 years
    YEAH Absolutely this resolved my both the issue, and now both the codes are working together by just using single line you added in code --- magic, but not getting Camera Preview, why?
  • Android
    Android over 10 years
    ok let me check, but i have resolved your problem, what you asked here, so i think you should accept my answer...
  • Sun
    Sun over 10 years
    yeah i will accept your answer for sure, because that's correct, but just give me some time....
  • praveen
    praveen over 10 years
    @AbrahimNeil let me know if you are still facing problem in solving your issue
  • gitesh.tyagi
    gitesh.tyagi over 9 years
    above code fixed the error because you removed the callback, so surfaceCreated never getting called again so No exception and since it is not getting called that's why No camera preview! Kindly refer stackoverflow.com/a/16487366/962545 for details.
  • Eduardo Gomez
    Eduardo Gomez almost 9 years
    Sweet response. Based on work around from Feb 18, 2010: code.google.com/p/android/issues/detail?id=6201
  • cocobear
    cocobear about 7 years
    This solution help me .When I add codes in onStop, it never get error.