Android camera locked after force close

2019-01-24 06:06发布

问题:

I have an application where I use devices camera.

Now I only release camera in normal flow.

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    if(camera != null) {
        camera.stopPreview();
        camera.release();
    } 
} 

Thus, then application exits in camera mode in unexpected way - i.e. Force Close (due to OutOfMemoryError) - camera gets locked. And only way to release it is to restart device.

And after application is started I get: RuntimeException: Fail to connect to camera service

How could I make sure, that camera is released in any case?

回答1:

Since the best way to keep a portion of code so that you find it later is to publish it in the 'net,

private UnexpectedTerminationHelper mUnexpectedTerminationHelper = new UnexpectedTerminationHelper();
private class UnexpectedTerminationHelper {
    private Thread mThread;
    private Thread.UncaughtExceptionHandler mOldUncaughtExceptionHandler = null;
    private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) { // gets called on the same (main) thread
            XXXX.closeCamera(); // TODO: write appropriate code here
            if(mOldUncaughtExceptionHandler != null) {
                // it displays the "force close" dialog
                mOldUncaughtExceptionHandler.uncaughtException(thread, ex);
            }
        }
    };
    void init() {
        mThread = Thread.currentThread();
        mOldUncaughtExceptionHandler = mThread.getUncaughtExceptionHandler();
        mThread.setUncaughtExceptionHandler(mUncaughtExceptionHandler);
    }
    void fini() {
        mThread.setUncaughtExceptionHandler(mOldUncaughtExceptionHandler);
        mOldUncaughtExceptionHandler = null;
        mThread = null;
    }
}

and, in the appropriate places on the main thread:

    mUnexpectedTerminationHelper.init();

and

    mUnexpectedTerminationHelper.fini();


回答2:

As @CommonsWare suggested, if you want to make sure, that your application releases Camera before crashing you should use Thread.setDefaultUncaughtExceptionHandler().

Or if you're using Bugsense:

private class MyCameraActivity extends Activity implements SurfaceHolder.Callback, ExceptionCallback {

    @Override
    protected void onCreate(Bundle sSavedInstanceState) {
        super.onCreate(sSavedInstanceState);

        BugSenseHandler.setExceptionCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        this.camera = open();

        try {
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }

        // other camera stuff
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        releaseCamera();
    }

    @Override
    public void lastBreath() {
        releaseCamera();
    }

    private void releaseCamera() {
        if (camera != null) {
            camera.stopPreview();
            camera.release();
        }
    }
} 


回答3:

Out of memory error comes because the image you tried to save exeeded the limit, normally comes when you try to process a bitmap.

You can try these methods to avoid camera getting not released.

Camera myCamera;

Use logs and fing where the error occurs and Add a try catch block and in the catch block add myCamera.stopPreview(); and myCamera.release(); Also override the ondestroy and onpause method and add myCamera.stopPreview(); and myCamera.release();

Hope this helps you or atleast gave you an idea.



回答4:

private void stopPreviewAndFreeCamera() {

if (mCamera != null) {
    /*
      Call stopPreview() to stop updating the preview surface.
    */
    mCamera.stopPreview();

    /*
      Important: Call release() to release the camera for use by other applications. 
      Applications should release the camera immediately in onPause() (and re-open() it in
      onResume()).
    */
    mCamera.release();

    mCamera = null;
}}

do it like this.