在调用Camera.release()之后正在使用Camera [英] Camera is being used after Camera.release() was called

查看:854
本文介绍了在调用Camera.release()之后正在使用Camera的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中按下拍照"按钮后,相机崩溃并显示以下错误消息:

After pressing the take a picture button in my Android app, the camera crashes with the following error message:

E 03/21/2016 10:29:49:164 000007d1 CameraObject| Camera is being used after Camera.release() was called
java.lang.RuntimeException: Camera is being used after Camera.release() was called
at android.hardware.Camera._stopPreview(Native Method)
at android.hardware.Camera.stopPreview(Camera.java:730)
at com.rho.camera.CameraObject.stopPreview(CameraObject.java:435)
at com.rho.camera.CameraActivity.onDestroy(CameraActivity.java:131)
at android.app.Activity.performDestroy(Activity.java:6407)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1142)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3818)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3849)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

该应用是使用Rhomobile 5.4开发的,相关的源代码在GitHub上:

The app is developed using Rhomobile 5.4 and the relevant source code is on GitHub:

  • CaemraActivity:
  • CaemraActivity: https://github.com/rhomobile/rhodes/blob/5-4-stable/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraActivity.java
  • CameraObject: https://github.com/rhomobile/rhodes/blob/5-4-stable/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraObject.java

我将Rhomobile 5.4与以下SDK结合使用: -minSDK 21 -Android SDK 5.1.1 -Android NDK 10e -Java 7 u80 SDK -在OS X上

I'm using Rhomobile 5.4 with the following SDKs: - minSDK 21 - Android SDK 5.1.1 - Android NDK 10e - Java 7 u80 SDK - On OS X

我对Android开发不熟悉.

I'm not familiar with Android development.

推荐答案

这确实过期,但由于我在一分钟前设法解决了我的类似问题,我认为我会为自己和其他人的利益做出贡献谁会拼命搜索Stack.

This is really overdue but as I managed to solve a similar problem of mine a minute ago, I thought I'd contribute for the benefit of yourself and anyone else who might be desperately searching Stack.

因此,旋转设备时,您将在释放相机的同时调用onPause和onDestroy.我注意到您的onResume中确实有camera.open(),而没有看您无法评论的与表面相关的代码.这对我有用.

So when you rotate the device, you are calling onPause and onDestroy, where you are releasing your camera. I noticed you do have camera.open() in your onResume, without a look at your surface-related code I cannot comment. Heres what worked for me.

首先,cameraPreview

Firstly, the cameraPreview

public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
try {
    this.mCamera.setPreviewDisplay(surfaceHolder);
    this.mCamera.startPreview();
} catch (Exception e) {
}
}


public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
    //TODO we need this here too because on SurfaceCreated we always need to open the camera, in case its released

    this.mCamera.setPreviewDisplay(surfaceHolder);
    this.mCamera.setDisplayOrientation(90);
    //this.mCamera.startPreview();
} catch (IOException e) {
}
}

接下来,CameraActivity

Next, the CameraActivity

@Override
public void onResume() {
    super.onResume();
   try{
       mCamera = openFrontFacingCameraGingerbread();
      // Add to Framelayout
       this.mCameraPreview = new CameraPreview(this, this.mCamera);
        mImage.removeAllViews();
       this.mImage.addView(this.mCameraPreview);

   }catch (RuntimeException ex){

    }



}


@Override
public void onPause() {
    super.onPause();
    captureButton.setText("Begin Capture");
    if(CameraActivity.this.timer !=null) {
        CameraActivity.this.timer.cancel();
        CameraActivity.this.timer.purge();
        CameraActivity.this.timer = null;
    }
    if (mCamera != null) {
        mCamera.setPreviewCallback(null);
        mCameraPreview.getHolder().removeCallback(mCameraPreview);
        mCamera.release();
        mCamera = null;
    }


}


@Override
protected void onDestroy(){
    super.onDestroy();
    releaseCameraAndPreview();
}

private void releaseCameraAndPreview() {

    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
    if(mCameraPreview != null){
        mCameraPreview.destroyDrawingCache();
        mCameraPreview.mCamera = null;
    }
}

这篇关于在调用Camera.release()之后正在使用Camera的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆