强行关闭后Android相机锁定

时间:2013-06-26 11:20:13

标签: android android-camera

我有一个使用设备相机的应用程序。

现在我只在正常流程中释放相机。

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

因此,应用程序以意外方式退出相机模式 - 即强制关闭(由于OutOfMemoryError) - 相机被锁定。释放它的唯一方法是重启设备。

申请开始后,我得到: RuntimeException: Fail to connect to camera service

我怎么能确定,相机在任何情况下都会被释放?

4 个答案:

答案 0 :(得分:3)

正如@CommonsWare建议的那样,如果你想确保你的应用程序在崩溃之前发布了Camera,你应该使用Thread.setDefaultUncaughtExceptionHandler()

或者如果您使用的是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();
        }
    }
} 

答案 1 :(得分:3)

由于保留一部分代码以便以后找到它的最佳方法是将其发布在'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;
    }
}

并且,在主线程的适当位置:

    mUnexpectedTerminationHelper.init();

    mUnexpectedTerminationHelper.fini();

答案 2 :(得分:0)

出现内存不足错误,因为您尝试保存的图像超出限制,通常在您尝试处理位图时出现。

您可以尝试这些方法以避免相机未被释放。

相机myCamera;

使用发生错误的日​​志和文件 添加try catch块并在catch块中添加myCamera.stopPreview();myCamera.release(); 同时覆盖ondestroy和onpause方法以及add myCamera.stopPreview();myCamera.release();

希望这可以帮助你或至少给你一个想法。

答案 3 :(得分:-3)

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

这样做。