gpt4 book ai didi

Android:当 Activity 进入后台时如何使用相机?

转载 作者:行者123 更新时间:2023-11-30 03:46:06 25 4
gpt4 key购买 nike

当我的 Activity 进入后台时,如何释放相机? (例如按手机上的待机键)

    public void onPause(){
super.onPause();
if (cam != null) {
cam.stopPreview();
cam.release();
cam = null;
}
}

@Override
protected void onResume(){
super.onResume();
rLayout = (RelativeLayout) findViewById (R.id.layout_background);
cam = Camera.open();
cam.startPreview();
p = cam.getParameters();
}

日志:

02-20 11:46:40.040: E/AndroidRuntime(26439): FATAL EXCEPTION: main
02-20 11:46:40.040: E/AndroidRuntime(26439): java.lang.RuntimeException: Unable to resume activity {spicysoftware.spicytaschenlampe/spicysoftware.spicytaschenlampe.FlashLight}: java.lang.RuntimeException: Fail to connect to camera service
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.os.Looper.loop(Looper.java:137)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-20 11:46:40.040: E/AndroidRuntime(26439): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 11:46:40.040: E/AndroidRuntime(26439): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 11:46:40.040: E/AndroidRuntime(26439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-20 11:46:40.040: E/AndroidRuntime(26439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-20 11:46:40.040: E/AndroidRuntime(26439): at dalvik.system.NativeStart.main(Native Method)
02-20 11:46:40.040: E/AndroidRuntime(26439): Caused by: java.lang.RuntimeException: Fail to connect to camera service
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.hardware.Camera.native_setup(Native Method)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.hardware.Camera.<init>(Camera.java:353)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.hardware.Camera.open(Camera.java:330)
02-20 11:46:40.040: E/AndroidRuntime(26439): at spicysoftware.spicytaschenlampe.FlashLight.onResume(FlashLight.java:182)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.Activity.performResume(Activity.java:5280)
02-20 11:46:40.040: E/AndroidRuntime(26439): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
02-20 11:46:40.040: E/AndroidRuntime(26439): ... 12 more

这是它的工作原理:

    public void onPause(){
super.onPause();
Log.v("OnPause", "True");
if (cam != null) {
Log.v("OnPause", "Cam isn't null");
cam.stopPreview();
Log.v("OnPause", "CamPreview stopped");
cam.release();
Log.v("OnPause", "CamReleased");
cam = null;
Log.v("OnPause", "Cam is null");
}
}


@Override
protected void onResume(){
super.onResume();
Log.v("OnResume", "True");
if(cam != null){
Log.v("OnResume", "Camera is in use");
} else {
Log.v("OnResume", "Camera isn't in use");
rLayout = (RelativeLayout) findViewById (R.id.layout_background);
cam = Camera.open();
Log.v("OnResume", "Camera has been opened");
cam.startPreview();
Log.v("OnResume", "CamPreview started");
p = cam.getParameters();
Log.v("OnResume", "Wrote parameters to variable p");
}
}

在 onResume 中打开相机会使应用程序崩溃。

最好的问候MSeiz5

最佳答案

在 onResume 中重新打开并重置相机。

@Override
protected void onResume(){
super.onResume();
camera = Camera.open();

// We resumed so lock the camera
Camera.Parameters params = camera.getParameters();

params.setFlashMode(Parameters.FLASH_MODE_ON);
//params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
//params.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
params.setJpegQuality(100);

camera.setParameters(params);
ImageView overlay = (ImageView) findViewById(R.id.ImageView01);
overlay.bringToFront();
}

关于Android:当 Activity 进入后台时如何使用相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14976415/

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