Android 6.0 RuntimeException: Fail to connect to camera service
Solution 1
Write in your gradle.build targetSdkVersion 22. It is a temporary solution but work.
Solution 2
open Settings -> Apps -> click you app -> Permissions -> open Camera permissions.
you can see:
http://developer.android.com/training/permissions/requesting.html
Solution 3
This happens because, in Android 6.0 the permission model is different. You have to implement the new permission model which asks for the permission on run time. Even if you don't ask it, you can manually enable it in the phone's app setting, but that is not gona work when you publish your app in the play store. Here is an Article on How to get Run time permissions
Also you can check Nick's answer here for getting multiple permissions
Solution 4
From android 6.0 you can handle the app permission weather you will give or not specific permission for an application. In your case if you didn't enable camera permission for your app then this issues may arise.
So, you need to enable permission from settings->apps->your_app->enable camera
permission.
Solution 5
Add Camera Permission before Opening the Camera:
follow the Link for adding Permissions: https://developer.android.com/training/permissions/requesting.html
Related videos on Youtube
Comments
-
HelloSilence over 3 years
An error occurred with my app which ran in Nexus5 (Android6.0).
The error was
java.lang.RuntimeException: Fail to connect to camera service
I had added the permission to the AndroidManifest.xml.
But the app is OK with another phone(Android5.1), and genymotion AVD(Android 4.0).
The key code is below
@Override public void onResume() { super.onResume(); try { mCamera = Camera.open(); }catch (Exception e){ Log.e(TAG,"open camera failed",e); } }
-
Shadow Droid over 8 yearscheck this stackoverflow.com/questions/33030933/…
-
reVerse over 8 yearsPlease add the whole Stacktrace. I guess you got a
SecurityException
as well which is an indicator that you didn't request the camera permission at runtime.
-
-
Ilie Daniel Stefan over 8 yearsThis was also the solution for me. Very weird! What could be the cause?
-
c0dehunter about 8 yearsIt's not weird at all. API levels <=22 don't use run time permission, but 23 do. Check the link in zzzmode's answer.
-
Rahul Sonone about 8 yearsChanging Target sdk to 22 is not a solution because it will still fail for android 6 devices, go for proper implementation with android 6 way follow this llink developer.android.com/training/permissions/requesting.html
-
Houssem over 7 yearsYou saved my day ! But, it seem that we have to ask for permissions explicitly as @Amalan Dhananjayan otherwise the final user will not be supposed to know some tricky things as settings->apps->your_app->enable camera
-
0xAliHn over 7 years@Houssem Yes, you need to handle this programmatically. inthecheesefactory.com/blog/… a good tutorial to do this.
-
Savan Kachhiya Patel over 7 yearsInstead of these apply runtime permission for get better result and you can also use this in above 6.0