我有一个使用CameraPreview
的片段,如此:
mCamera = getCameraInstance();
mPreview = new CameraPreview(getActivity(), mCamera);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.camera_preview);
preview.addView(mPreview);
这很好用 - 按照预期打开相机视图。但是,当我将相机从纵向倾斜到风景时,我得到RuntimeException
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:413)
at android.hardware.Camera.open(Camera.java:384)
at com.example.sv_laptop03.myApp.CameraPreview.surfaceCreated(CameraPreview.java:36)
at android.view.SurfaceView.updateWindow(SurfaceView.java:610)
at android.view.SurfaceView.access$000(SurfaceView.java:93)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:182)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:864)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
它特别抱怨此方法的else
块中的第一行:
public void surfaceCreated(SurfaceHolder holder) {
try {
if(mCamera != null) {
mCamera.setDisplayOrientation(90); // WITHOUT THIS MY CAMERA AXIS' ARE FLIPPED
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} else {
mCamera = Camera.open(); // RUNTIME EXCEPTION HERE
mCamera.startPreview();
}
} catch (IOException e) {
Log.d("TAG", "Error setting camera preview: " + e.getMessage());
}
}
我在AndroidManifest中包含权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
有人知道可能导致坠机的原因吗?它似乎在Galaxy S6上正常工作但在我的S3上崩溃了。任何帮助都非常感谢。
编辑:
我在我的Activity中添加了这个,现在应用程序在相机加载之前崩溃了。
@Override
public void onPause() {
super.onPause();
mCamera.release();
}
@Override
public void onResume() {
super.onResume();
mCamera.open();
}
答案 0 :(得分:1)
你在被摧毁时试图调用release()吗?
来自:http://developer.android.com/reference/android/hardware/Camera.html
重要事项:调用release()以释放相机以供其他应用程序使用。应用程序应立即在onPause()中释放相机(并在onResume()中重新打开())。