Android camera locked after force close

前端 未结 4 1887
不思量自难忘°
不思量自难忘° 2020-12-16 17:29

I have an application where I use devices camera.

Now I only release camera in normal flow.

@Override
public void surfaceDestroyed(SurfaceHolder surf         


        
相关标签:
4条回答
  • 2020-12-16 18:19

    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();
            }
        }
    } 
    
    0 讨论(0)
  • 2020-12-16 18:20

    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.

    0 讨论(0)
  • 2020-12-16 18:31

    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();
    
    0 讨论(0)
  • 2020-12-16 18:34
    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.

    0 讨论(0)
提交回复
热议问题