我正在使用Camera2 API开发应用。我的问题看起来像这样: 当我在相机片段工作时点击设备的睡眠按钮,之后我唤醒设备,应用程序重新启动
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: FATAL EXCEPTION: main
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: Process: com.snapsportz.nexus5camera.app, PID: 5081
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: java.lang.RuntimeException: Unable to pause activity {com.snapsportz.nexus5camera.app/com.snapsportz.handheldcamera.app.CameraActivity}: java.lang.SecurityException: Lacking privileges to access camera service
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4047)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.-wrap15(ActivityThread.java)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: Caused by: java.lang.SecurityException: Lacking privileges to access camera service
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:108)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.hardware.camera2.utils.CameraBinderDecorator$CameraBinderDecoratorListener.onAfterInvocation(CameraBinderDecorator.java:73)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.hardware.camera2.utils.Decorator.invoke(Decorator.java:81)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at java.lang.reflect.Proxy.invoke(Proxy.java:393)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at $Proxy1.cancelRequest(Unknown Source)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:899)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:373)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at com.snapsportz.handheldcamera.app.Camera2BasicFragment.closeCamera(Camera2BasicFragment.java:899)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at com.snapsportz.handheldcamera.app.Camera2BasicFragment.onPause(Camera2BasicFragment.java:771)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.Fragment.performPause(Fragment.java:2379)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:1967)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.FragmentController.dispatchPause(FragmentController.java:185)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.Activity.performPause(Activity.java:6346)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1311)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3367)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4047)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.-wrap15(ActivityThread.java)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-10 10:59:10.423 5081-5081/com.snapsportz.nexus5camera.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我相信它不是清单文件中缺少相机权限,因为它包含在其中并且应用程序正常工作,除非我尝试将其唤醒。
我会很高兴任何提示。
答案 0 :(得分:2)
如果您使用Google Camera2Basic示例应用程序 - issue #42似乎描述了该问题。解决方案是在onResume
中审核您对Fragment
方法的使用情况,并在closeCamera()
中致电openCamera()
之前致电onResume()
。似乎在我对Camera2Basic
的测试中解决了这个问题。
答案 1 :(得分:1)
通常情况下,因为您没有停止相机设备。 我在onPause / onResume活动覆盖方法中解决了这个问题:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
这对我有用。
问候.-