2017-04-12 68 views
1

經過這麼長時間的鬥爭。我發現問題無法找到解決方案。我有一個按鈕點擊它的活動啓動一個相機打開的片段。我遵循這個tutorial,並添加了錯誤日誌創建視圖的代碼。如何解決相機實例打開失敗?

P.S:我已經添加了所有權限。

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
 
          Bundle savedInstanceState) { 
 
     View view = inflater.inflate(R.layout.fragment_scan, container, false); 
 

 
     if(checkCameraHardware(getActivity().getApplicationContext())){ 
 
      getCameraInstance(); 
 
      mCamera = getCameraInstance(); 
 
      mPreview = new CameraPreview(this.getActivity(), mCamera); 
 

 
      //container.addView(mPreview);--->Remove this line 
 
      //Add this line 
 
      FrameLayout preview =(FrameLayout)view.findViewById(R.id.camera_preview); 
 
      preview.addView(mPreview); 
 
     } 
 
     // Inflate the layout for this fragment 
 
     return view; 
 
    }

public void surfaceCreated(SurfaceHolder holder) { 
 
     // The Surface has been created, now tell the camera where to draw the preview. 
 
     try { 
 
      mCamera.setPreviewDisplay(holder); 
 
      mCamera.startPreview(); 
 
     } catch (IOException e) { 
 
      Log.d(TAG, "Error setting camera preview: " + e.getMessage()); 
 
     } 
 
    }

//permissions 
 

 
    <uses-permission android:name="android.permission.CAMERA"></uses-permission> 
 
    <uses-feature android:name="android.hardware.camera.autofocus" />

//error log 
 

 
D/tag: C not null 
 
W/CameraBase: An error occurred while connecting to camera: 0 
 
D/tag2: cannot get Camera Instance 
 
W/System.err: java.lang.RuntimeException: Fail to connect to camera service 
 
W/System.err:  at android.hardware.Camera.native_setup(Native Method) 
 
W/System.err:  at android.hardware.Camera.<init>(Camera.java:469) 
 
W/System.err:  at android.hardware.Camera.open(Camera.java:442) 
 
W/System.err:  at layout.scan.getCameraInstance(scan.java:113) 
 
W/System.err:  at layout.scan.onCreateView(scan.java:84) 
 
W/System.err:  at android.app.Fragment.performCreateView(Fragment.java:1700) 
 
W/System.err:  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 
 
W/System.err:  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 
 
W/System.err:  at android.app.BackStackRecord.run(BackStackRecord.java:698) 
 
W/System.err:  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 
 
W/System.err:  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) 
 
W/System.err:  at android.os.Handler.handleCallback(Handler.java:808) 
 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:103) 
 
W/System.err:  at android.os.Looper.loop(Looper.java:193) 
 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5330) 
 
W/System.err:  at java.lang.reflect.Method.invokeNative(Native Method) 
 
W/System.err:  at java.lang.reflect.Method.invoke(Method.java:515) 
 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
 
W/System.err:  at dalvik.system.NativeStart.main(Native Method) 
 
D/SurfaceView: checkGLSurfaceViewlLogProperty get invalid command 
 
I/SurfaceView: updateWindow -- onWindowVisibilityChanged, visibility = 0 
 
I/Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread. 
 
D/GraphicBuffer: close handle(0x6169bce0) (w:720 h:1280 f:1) 
 
D/GraphicBuffer: close handle(0x623215a8) (w:720 h:1280 f:1) 
 
D/GraphicBuffer: close handle(0x61daa2a8) (w:720 h:1280 f:1) 
 
D/GraphicBuffer: close handle(0x61db12f8) (w:720 h:1280 f:1) 
 
D/GraphicBuffer: create handle(0x61db12f8) (w:720, h:1280, f:1) 
 
I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0 
 
I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1 
 
I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3 
 
D/GraphicBuffer: close handle(0x61db12f8) (w:720 h:1280 f:1) 
 
D/GraphicBuffer: create handle(0x61db12f8) (w:720, h:1280, f:1) 
 
I/SurfaceView: updateWindow -- setFrame 
 
I/SurfaceView: updateWindow -- OnPreDrawListener, mHaveFrame = true 
 
I/SurfaceView: Changes: creating=true format=true size=true visible=true left=true top=true mUpdateWindowNeeded=false mReportDrawNeeded=false redrawNeeded=false forceSizeChanged=true mVisible=false mRequestedVisible=true 
 
I/SurfaceView: Cur surface: Surface(name=null)/@0x42481d80 
 
V/SurfaceView: layout.CameraPreview{42481ad8 V.E..... ......ID 0,0-720,1118} got resized: w=720 h=1118, cur w=-1 h=-1 
 
I/SurfaceView: New surface: Surface(name=null)/@0x42481e50, vis=true, frame=Rect(0, 162 - 720, 1280) 
 
I/SurfaceView: Callback --> surfaceCreated 
 
I/SurfaceView: surfaceCreated callback + 
 
I/SurfaceView: finishedDrawing 
 
D/AndroidRuntime: Shutting down VM 
 
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a2fcf8) 
 
W/dalvikvm: threadid=1: uncaught exception occurred 
 
W/System.err: java.lang.NullPointerException 
 
W/System.err:  at layout.CameraPreview.surfaceCreated(CameraPreview.java:37) 
 
W/System.err:  at android.view.SurfaceView.updateWindow(SurfaceView.java:662) 
 
W/System.err:  at android.view.SurfaceView.access$000(SurfaceView.java:90) 
 
W/System.err:  at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:195) 
 
W/System.err:  at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) 
 
W/System.err:  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2191) 
 
W/System.err:  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189) 
 
W/System.err:  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223) 
 
W/System.err:  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) 
 
W/System.err:  at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
 
W/System.err:  at android.view.Choreographer.doFrame(Choreographer.java:560) 
 
W/System.err:  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774) 
 
W/System.err:  at android.os.Handler.handleCallback(Handler.java:808) 
 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:103) 
 
W/System.err:  at android.os.Looper.loop(Looper.java:193) 
 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5330) 
 
W/System.err:  at java.lang.reflect.Method.invokeNative(Native Method) 
 
W/System.err:  at java.lang.reflect.Method.invoke(Method.java:515) 
 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
 
W/System.err:  at dalvik.system.NativeStart.main(Native Method) 
 
W/dalvikvm: threadid=1: calling UncaughtExceptionHandler 
 
E/AndroidRuntime: FATAL EXCEPTION: main 
 
        Process: com.example.root.meeransunday, PID: 3179 
 
        java.lang.NullPointerException 
 
         at layout.CameraPreview.surfaceCreated(CameraPreview.java:37) 
 
         at android.view.SurfaceView.updateWindow(SurfaceView.java:662) 
 
         at android.view.SurfaceView.access$000(SurfaceView.java:90) 
 
         at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:195) 
 
         at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) 
 
         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2191) 
 
         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189) 
 
         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223) 
 
         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) 
 
         at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
 
         at android.view.Choreographer.doFrame(Choreographer.java:560) 
 
         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774) 
 
         at android.os.Handler.handleCallback(Handler.java:808) 
 
         at android.os.Handler.dispatchMessage(Handler.java:103) 
 
         at android.os.Looper.loop(Looper.java:193) 
 
         at android.app.ActivityThread.main(ActivityThread.java:5330) 
 
         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:828) 
 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
 
         at dalvik.system.NativeStart.main(Native Method)

//One more method Just to add detail 
 
    public static Camera getCameraInstance(){ 
 
     Camera c = null; 
 
     try { 
 

 
      c = Camera.open(); // attempt to get a Camera instance 
 
     if(c!=null) 
 
     { 
 
      Log.d("tag"," C not null"); 
 
     } 
 
     } 
 
     catch (Exception e) 
 
     { 
 
      Log.d("tag2","cannot get Camera Instance"); 
 
      e.printStackTrace(); 
 
     } 
 
     return c; // returns null if camera is unavailable 
 
    }

+0

你爲什麼要調用'getCameraInstance();'兩次?在'onCreateView' –

+0

我沒有得到你..你能詳細說明 –

回答

0

要使用下面的方法

android.hardware.Camera.open(int cameraId) 

你應該通過cameraId,如果你想前置攝像頭編號,你可以用下面的方法

private int findFrontFacingCamera() { 

    // Search for the front facing camera 
    int numberOfCameras = Camera.getNumberOfCameras(); 
    for (int i = 0; i < numberOfCameras; i++) { 
     CameraInfo info = new CameraInfo(); 
     Camera.getCameraInfo(i, info); 
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
      cameraId = i; 
      cameraFront = true; 
      break; 
     } 
    } 
    return cameraId; 
} 

如果其他應用程序打開相同的相機,則會引發RuntimeException。 完成使用相機後,您必須調用release(),否則它將保持鎖定狀態並對其他應用程序不可用。 您的應用程序應該一次只有一個Camera對象用於特定的硬件攝像頭。 如果這不起作用,讓我知道。

+0

鞋消息ok取消方法不可用你可以通過那 –

+0

這不是我不使用marshmellow的問題我正在測試API 19和22。它。不工作 –

+0

已添加showMessageOKCancel請檢查 – Shivam