我遇到了关于Camera.open()的问题。它抛出异常。
# [1,] 1 0
# [2,] 0 5
# [3,] 0 9
# [4,] 13 0
# [5,] 0 17
# [6,] 0 21
# [7,] 0 25
# [8,] 29 0
我写的代码:
07-04 10:38:52.039 22582-22582/test.videorecorder E/Camera numbers:﹕ 2
07-04 10:38:52.049 22582-22582/test.videorecorder E/Camera 0﹕ 1
07-04 10:38:52.049 22582-22582/test.videorecorder E/Camera 1﹕ 0
07-04 10:38:52.559 22582-22582/test.videorecorder E/init camera﹕ can't open the back camera
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:370)
at android.hardware.Camera.open(Camera.java:329)
at test.videorecorder.MainActivity.initCamera(MainActivity.java:72)
at test.videorecorder.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)
at dalvik.system.NativeStart.main(Native Method)
07-04 10:38:52.779 22582-22582/test.videorecorder E/preview holder﹕ surface create
07-04 10:38:52.779 22582-22582/test.videorecorder E/preview holder﹕ 1 times create
07-04 10:38:52.789 22582-22582/test.videorecorder E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: test.videorecorder, PID: 22582
java.lang.NullPointerException
at test.videorecorder.MainActivity$2.surfaceCreated(MainActivity.java:115)
at android.view.SurfaceView.updateWindow(SurfaceView.java:664)
at android.view.SurfaceView.access$000(SurfaceView.java:93)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:191)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1888)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1017)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)
at dalvik.system.NativeStart.main(Native Method)
AndroidManifest.xml我添加了这些行:
private void initCamera() {
try {
int defaultBackCamera = 0;
Log.e("Camera numbers: " , "" + Camera.getNumberOfCameras());
Camera.CameraInfo info = new Camera.CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); i ++) {
Camera.getCameraInfo(i, info);
Log.e("Camera " + i, "" + info.facing);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
defaultBackCamera = i;
}
mCamera = Camera.open(defaultBackCamera);
} catch (Exception e) {
Log.e("init camera", "can't open the back camera", e);
if (mCamera != null) {
mCamera.release();;
mCamera = null;
}
}
}
我看了很多关于打开相机失败的问题,但没有人可以解决我的问题,所以我想知道原因以及如何解决它。