我已经使用了所有需要的权限,它们是:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
我使用的代码是:
private Camera openFrontFacingCameraGingerbread()
{ int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++)
{ Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{ try { cam = Camera.open(camIdx);
} catch (RuntimeException e)
{ Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
} return cam;
}
但是错误即将出现。日志错误:
Cannot connect camera service
Logcat 用于以下提供的解决方案,
FATAL EXCEPTION: main
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: Process: pop.lock.com.frocam, PID: 4090
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View$1.onClick(View.java:3848)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View.performClick(View.java:4463)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18770)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:808)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:103)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5333)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View$1.onClick(View.java:3843)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View.performClick(View.java:4463)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18770)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:808)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:103)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5333)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: Caused by: java.lang.NullPointerException
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at pop.lock.com.frocam.FrontCamActivity.onClick(FrontCamActivity.java:41)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View$1.onClick(View.java:3843)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View.performClick(View.java:4463)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18770)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:808)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:103)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
我正在使用 compilesdkversion 23,使用我的 4.4.2 kitkat HTC 816g 手机来运行应用程序。任何与相机服务相关的代码都没有执行。