我有一个应用程序,我在onResume()
方法中打开相机:
@Override
public void onResume() {
super.onResume();
openCamera();
....
}
public void openCamera() {
try {
camera = Camera.open();
Log.d(CAMERA, "Camre opened");
} catch (Exception exception) {
Log.e(CAMERA, "Cannot open camera", exception);
}
}
如果我不在方法中调用 openCamera() 方法,它可以正常工作onResume()
,但在onResume()
方法中它给了我以下异常:
08-15 14:50:33.999: E/camera(1753): Cannot open camera
08-15 14:50:33.999: E/camera(1753): java.lang.RuntimeException: Fail to connect to camera service
08-15 14:50:33.999: E/camera(1753): at android.hardware.Camera.native_setup(Native Method)
08-15 14:50:33.999: E/camera(1753): at android.hardware.Camera.<init>(Camera.java:302)
08-15 14:50:33.999: E/camera(1753): at android.hardware.Camera.open(Camera.java:279)
08-15 14:50:33.999: E/camera(1753): at buet.threebyzero.autoSecuritySystem.Capture.openCamera(Capture.java:46)
08-15 14:50:33.999: E/camera(1753): at buet.threebyzero.autoSecuritySystem.Capture.onResume(Capture.java:55)
08-15 14:50:33.999: E/camera(1753): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
08-15 14:50:33.999: E/camera(1753): at android.app.Activity.performResume(Activity.java:4539)
08-15 14:50:33.999: E/camera(1753): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
08-15 14:50:33.999: E/camera(1753): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
08-15 14:50:33.999: E/camera(1753): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
08-15 14:50:33.999: E/camera(1753): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-15 14:50:33.999: E/camera(1753): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-15 14:50:33.999: E/camera(1753): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 14:50:33.999: E/camera(1753): at android.os.Looper.loop(Looper.java:137)
08-15 14:50:33.999: E/camera(1753): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-15 14:50:33.999: E/camera(1753): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 14:50:33.999: E/camera(1753): at java.lang.reflect.Method.invoke(Method.java:511)
08-15 14:50:33.999: E/camera(1753): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-15 14:50:33.999: E/camera(1753): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-15 14:50:33.999: E/camera(1753): at dalvik.system.NativeStart.main(Native Method)
编辑:重新启动模拟器和覆盖onPause()
方法后问题部分解决:
@Override
protected void onPause() {
super.onPause();
if(camera != null)
camera.release();
}