在我的活动中,我有3个Tabost,第一个是画廊第二个是相机,第三个是视频,当我点击相机打开时,用户可以轻松拍摄其工作正常,并点击另一个tabhost其他事情发生但当我再次点击在相机上,然后应用程序崩溃,它显示错误" OnResume相机错误..无法连接到相机服务"
下面是我的简历代码....
protected void onResume() {
super.onResume();
// TODO Auto-generated method stub
if(camera==null){
camera = Camera.open();
camera.startPreview();
camera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera mcamera) {
camera.release();
camera = Camera.open();
Log.d("Camera died", "error camera");
}
});
}
if (camera != null) {
if (Build.VERSION.SDK_INT >= 14)
setCameraDisplayOrientation(context,
CameraInfo.CAMERA_FACING_BACK, camera);
preview.setCamera(camera);
}
}
主要是在这行相机= Camera.open();
中显示错误我尝试在onPause中使用相机发布但没有工作..请提出建议
Logcat:
09-18 15:35:29.595: E/AndroidRuntime(9598): FATAL EXCEPTION: main
09-18 15:35:29.595: E/AndroidRuntime(9598): Process: com.example.rooshan, PID: 9598
09-18 15:35:29.595: E/AndroidRuntime(9598): java.lang.RuntimeException: Unable to resume activity {com.example.rooshan/com.rooshan.main.Photo_Activity}: java.lang.RuntimeException: Fail to connect to camera service
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2955)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:146)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.view.View.performClick(View.java:4478)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.view.View$PerformClick.run(View.java:18698)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.os.Handler.handleCallback(Handler.java:733)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.os.Handler.dispatchMessage(Handler.java:95)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.os.Looper.loop(Looper.java:149)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.ActivityThread.main(ActivityThread.java:5257)
09-18 15:35:29.595: E/AndroidRuntime(9598): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 15:35:29.595: E/AndroidRuntime(9598): at java.lang.reflect.Method.invoke(Method.java:515)
09-18 15:35:29.595: E/AndroidRuntime(9598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-18 15:35:29.595: E/AndroidRuntime(9598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
09-18 15:35:29.595: E/AndroidRuntime(9598): at dalvik.system.NativeStart.main(Native Method)
09-18 15:35:29.595: E/AndroidRuntime(9598): Caused by: java.lang.RuntimeException: Fail to connect to camera service
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.hardware.Camera.native_setup(Native Method)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.hardware.Camera.<init>(Camera.java:407)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.hardware.Camera.open(Camera.java:345)
09-18 15:35:29.595: E/AndroidRuntime(9598): at com.rooshan.main.Photo_Activity.onResume(Photo_Activity.java:93)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.Activity.performResume(Activity.java:5490)
09-18 15:35:29.595: E/AndroidRuntime(9598): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2945)
09-18 15:35:29.595: E/AndroidRuntime(9598): ... 17 more
答案 0 :(得分:0)
您是否在onPause中释放了相机?
喜欢这个
if (mCamera != null) {
// Call stopPreview() to stop updating the preview surface.
mCamera.stopPreview();
// Important: Call release() to release the camera for use by other
// applications. Applications should release the camera immediately
// during onPause() and re-open() it during onResume()).
mCamera.release();
mCamera = null;
}
您必须停止预览并在您的情况下释放相机,否则重新启动仍然无法首次通话...