无法连接到Fragment中的摄像头服务

时间:2014-09-26 09:19:29

标签: android android-fragments android-camera

我得到了

  

无法连接到相机服务

当我打电话

int camerasNumber = Camera.getNumberOfCameras();

在我的片段中。

问题是如果我使用一个Activity就可以了!

这是我的片段:

public class QRFragment extends Fragment {

static {
  System.loadLibrary("iconv");
} 

public static Fragment newInstance(int position) {
QRFragment frag=new QRFragment();
return(frag);
}

@Override
public View onCreateView(LayoutInflater inflater,
                       ViewGroup container,
                       Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.qrfragment, container, false);
mCamera = getCameraInstance();
mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);
return(view);
}

public void onPause() {
  super.onPause();
  releaseCamera();
}

public static Camera getCameraInstance(){
  Camera c = null;
  try {
      c = Camera.open();
  } catch (Exception e){
  }
  if(c==null) {
    int camerasNumber = Camera.getNumberOfCameras();
    if(camerasNumber>0)c=Camera.open(0);
    }
  return c;
}

private void releaseCamera() {
  if (mCamera != null) {
      previewing = false;
      mCamera.setPreviewCallback(null);
      mCamera.release();
      mCamera = null;
  }
}

这是我的Logcat,

EXCEPTION: main
09-26 10:53:36.126: E/AndroidRuntime(24301): java.lang.RuntimeException: Fail to connect to camera service
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.hardware.Camera.native_setup(Native Method)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.hardware.Camera.<init>(Camera.java:410)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.hardware.Camera.open(Camera.java:365)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at com.example.ticketingaap.fragments.QRFragment.getCameraInstance(QRFragment.java:118)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at    com.example.ticketingaap.fragments.QRFragment.onCreateView(QRFragment.java:74)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.View.measure(View.java:16195)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.View.measure(View.java:16195)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at   android.view.View.measure(View.java:16195)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at  android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at     android.view.View.measure(View.java:16195)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2472)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.View.measure(View.java:16195)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2173)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1321)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1532)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1218)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4975)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.Choreographer.doFrame(Choreographer.java:561)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.os.Handler.handleCallback(Handler.java:725)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.os.Looper.loop(Looper.java:176)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at android.app.ActivityThread.main(ActivityThread.java:5279)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at java.lang.reflect.Method.invokeNative(Native Method)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at java.lang.reflect.Method.invoke(Method.java:511)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-26 10:53:36.126: E/AndroidRuntime(24301):    at dalvik.system.NativeStart.main(Native Method)

请让我知道我做错了什么?

提前致谢

1 个答案:

答案 0 :(得分:1)

请确保在不再需要时调用release()方法释放相机,否则您将无法使用Camera