gpt4 book ai didi

java - 调用 Camera.release() 后正在使用相机

转载 作者:可可西里 更新时间:2023-11-01 11:40:45 33 4
gpt4 key购买 nike

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

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:

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

我不熟悉 Android 开发。

最佳答案

这确实迟到了,但由于我在一分钟前设法解决了我的一个类似问题,我想我会为你自己和可能正在拼命搜索 Stack 的任何其他人的利益做出贡献。

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

首先是相机预览

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

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

关于java - 调用 Camera.release() 后正在使用相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36251691/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com