2017-05-29 115 views
0

E/AndroidRuntime:致命異常:主 了java.lang.RuntimeException:失敗在android.hardware.Camera.native_setup(本機方法) 連接到照相機服務 在android.hardware.Camera(Camera.java。 :423) at android.hardware.Camera.open(Camera.java:384) at ge.baqari.dev.capture.ui.view.CameraView.surfaceChanged(CameraView.kt:39) at android.view.SurfaceView .updateWindow(SurfaceView.java:617) at android.view.SurfaceView.access $ 000(SurfaceView.java:88) at android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:183) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:692) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123) at android.view.ViewRootImpl。 doTraversal(ViewRootImpl.java:1139) at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4879) at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:776) at android.view。 Choreographer.doCallbacks(Choreographer.java:579) at android.view.Choreographer.doFrame(Choreographer.java:548) at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreog rapher.java:762) 在android.os.Handler.handleCallback(Handler.java:725) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(活套。 java:153) at android.app.ActivityThread.main(ActivityThread.java:5341) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java :511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:929) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) at dalvik.system .NativeStart.main(本地方法)RuntimeException:無法連接到攝像頭服務?

我發現了很多答案,但他們都不幫我,我試圖創建自定義相機視圖,實現表面持有人,但它總是與Camera.open()方法,與參數和參數不一樣,崩潰崩潰,崩潰

class CameraView : android.view.SurfaceView, android.view.SurfaceHolder.Callback { 

    var camera: android.hardware.Camera? = null 
    var previewing = false 
    var cameraCallback: CameraCallBack? = null 
    var pictureCallback: PictureCallback? = null 
    var cameraParameters: android.hardware.Camera.Parameters? = null 

    private fun init() { 
     holder.addCallback(this) 
     holder.setType(SURFACE_TYPE_NORMAL) 
    } 

    constructor(context: android.content.Context, attrs: android.util.AttributeSet) : super(context, attrs) { 
     init() 
    } 

    override fun surfaceCreated(surfaceHolder: android.view.SurfaceHolder) { 

    } 

    override fun surfaceChanged(surfaceHolder: android.view.SurfaceHolder, i: Int, i1: Int, i2: Int) { 
     if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){ 
      if (previewing) { 
       camera?.stopPreview() 
       previewing = false 
      } 

      if (camera == null) { 
       try { 
        camera = Camera.open(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) 
        cameraParameters!!.setPreviewSize(i1, i2) 
        camera!!.parameters = cameraParameters!! 
        camera!!.startPreview() 
        camera!!.setPreviewDisplay(holder) 
        previewing = true 
       } catch (e: java.io.IOException) { 
        e.printStackTrace() 
       } 
      } 
     } 
    } 

    override fun surfaceDestroyed(surfaceHolder: android.view.SurfaceHolder) { 
     camera!!.stopPreview() 
     camera!!.release() 
     previewing = false; 
    } 
} 
+1

嘗試重新啓動設備。有時候,相機服務會被打開並且永遠不會關閉,並且從我找到的唯一一致的恢復方法是重新啓動設備。 – CommonsWare

+0

我已經重新啓動了2次,但處理過,嘗試過兩個android設備,但都是華爲設備 –

+0

發佈後,將camera var設置爲null。 –

回答

0

問題是CameraParameters,參數爲空,您嘗試設置爲null參數

修復:

camera = Camera.open(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) 
cameraParameters = camera!!.parameters 
cameraParameters!!.setPreviewSize(i1, i2) 
camera!!.parameters = cameraParameters!! 
camera!!.startPreview() 
camera!!.setPreviewDisplay(holder) 

你也可以使用這個庫camera preview

+0

不,應用程序在Camera.open()方法崩潰後 –

+1

@BaqarGogia您是否設置了相機權限? – Silvestr

+0

我沒有權限設置hardware.camera的功能 –

相關問題