gpt4 book ai didi

android - 如何在完成后重新打开 Activity 并在其中再次启动相机预览

转载 作者:行者123 更新时间:2023-11-30 01:23:15 25 4
gpt4 key购买 nike

在我的相机应用程序的名为 CameraActivity 的 Launcher Activity 中,我重写了 onPause() 方法,如下所示,

@Override
protected void onPause() {
super.onPause();
mCamera.release();
finish();
}

因此,无论何时通过按下主页按钮或后退按钮退出应用程序,相机都会被释放并且 Activity 完成以让其他相机应用程序访问相机。但现在我创建了另一个名为 CameraSettings 的 Activity 。并使用 Intent 打开它。问题是当我想返回 CameraActivity 应用程序关闭并返回主屏幕时,因为我在 onPause 中使用了 finish() 方法()。即使我不使用 finish() 方法,我仍然会收到错误消息,因为相机已经释放。

我想要的是,当我在 CameraActivity 中时,如果我按下 back/home,那么相机将被释放,如果我进入 CameraSettings Activity ,之后更改相机 我想返回到 CameraActivity,这将重新打开要使用的相机。我尝试覆盖 onResume() 方法并再次启动相机预览,但它给出了错误。下面是 onResume() 方法,

@Override
protected void onResume() {
super.onResume();
mCamera.startPreview();
}

这是我第一次启动相机预览的 onCreate() 方法,

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_test);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

//ShutterSound
final Camera.ShutterCallback shutter = new Camera.ShutterCallback() {
@Override
public void onShutter() {

}
};

//Settings listener
settings = (ImageView) findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent openSettings = new Intent(getApplicationContext(),CameraSettings.class);
startActivity(openSettings);
}
});



//Capture button listener
captureButton = (Button) findViewById(R.id.captureButton);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
captureButton.setEnabled(false);
// get an image from the camera
mCamera.takePicture(shutter, null, mPicture);
}
}
);

//Switch the cameras
switchCamera = (ImageView) findViewById(R.id.switchCamera);
switchCamera.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
checkFrontCameraInUse = !checkFrontCameraInUse;
switchCamera.setEnabled(false);
switchCamera();
}
});

/*
* Below code is
* needed to start the camera first time
* */

// Create an instance of Camera
mCamera = CameraInstance.getCameraInstance(0);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera, 0);
preview = (LinearLayout) findViewById(R.id.camera_preview);

assert preview != null;
preview.addView(mPreview);
}

这是错误信息,

    FATAL EXCEPTION: main
Process: com.prohelika.testcamera, PID: 28304
java.lang.RuntimeException: Camera is being used after Camera.release() was called
at android.hardware.Camera.setPreviewSurface(Native Method)
at android.hardware.Camera.setPreviewDisplay(Camera.java:801)
at com.prohelika.testcamera.CameraPreview.surfaceCreated(CameraPreview.java:36)
at android.view.SurfaceView.updateWindow(SurfaceView.java:675)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:273)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8998)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1190)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1190)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1190)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1190)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1190)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1190)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1575)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1296)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6602)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

如果我的解释不好,请原谅我。

最佳答案

由于您已经释放了相机资源,因此在 onResume() 方法中调用 startPreview() 方法时出现错误。

释放后将camera设置为null。并从相机预览中删除 previewCallback

    @Override
protected void onPause() {
super.onPause();
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release();
mCamera = nulll
finish();
}

尝试检查 camera 是否为 null 并在 onResume() 方法内重新初始化您的 camera 实例:

@Override
protected void onResume() {
super.onResume();
if (mCamera == null) {
mCamera = getCameraInstance();
}
mCamera.startPreview();
}

P.S. 如果 null,您可能需要重新初始化预览。

希望这对您有所帮助!

关于android - 如何在完成后重新打开 Activity 并在其中再次启动相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36786219/

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