2017-02-23 98 views
0

我想做一個簡單的手電筒應用程序,當我測試時,應用程序崩潰。我認爲嘗試訪問相機時出現了問題。有什麼建議麼?訪問相機時發生崩潰?

W/CameraBase: An error occurred while connecting to camera: 0 
W/CameraBase: An error occurred while connecting to camera: 0 
W/CameraBase: An error occurred while connecting to camera: 0 
W/CameraBase: An error occurred while connecting to camera: 0 
W/CameraBase: An error occurred while connecting to camera: 0 
I/Camera2Mode: fail 
E/AndroidRuntime: FATAL EXCEPTION: main 

java.lang.RuntimeException: Fail to connect to camera service 
at android.hardware.Camera.open(Camera.java:479) 
at android.hardware.Camera.open(Camera.java:547) 
at android.view.View.performClick(View.java:5226) 
at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
at android.view.View$PerformClick.run(View.java:21266) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95  
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5845) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 


at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

D/Process: killProcess, pid=23317 
D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:113 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 
Disconnected from the target VM, address: 'localhost:8603', transport: 'socket' 
+1

請張貼你的清單代碼。你給那些權限? –

+0

stackoverflow.com/questions/23904459/android-java-lang-runtimeexception-fail-to-connect-to-camera-service –

+1

嘗試在清單中使用這兩行 –

回答

1

我認爲你需要把相機許可清單文件一樣,可以鏈接到這個問題,這個

<uses-permission android:name="android.permission.CAMERA"/> 

其他的事情是指this鏈接

1

可以有兩個問題。其一是允許這是很容易handle.Also要小心,而在許可你正在開發Android 6.0的應用程序辦案就在清單中添加此:

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

另一種是當你試圖打開你的相機可能有是那個時候使用相機的另一個應用程序。 因此,爲避免異常,您需要在try-catch塊中打開相機。 這樣的:

static Camera camera = null; 
    try{ 
     camera = Camera.open(); 
     } catch(Exception e) { 
     Log.e("Error", ""+e); 
    } 
1

添加到您的清單

<uses-permission android:name="android.permission.CAMERA"/> 

和SDK 23及以上

<uses-permission-sdk-23 android:name="android.permission.CAMERA"/> 
0

如果您的測試手機是6.0 +,你應該用你的代碼的請求許可,否則你可以減少構建版本