Reputation: 13551
I have a phone specific problem when I open a camera on this one particular Nexus 5. Its the An error occurred while connecting to camera: 0 --- Fail to connect to camera service
error. On at least a dozen other phones everything works just fine. Other apps that use the camera on the Nexus 5 are not crashing (indicating its not all apps that can't access the camera, just mine).
There are a number of other questions on this and I have tried all of them. They all talk about missing permissions, and making sure the camera is destroyed after use.
To be clear my manifest requests and uses the camera properly:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />
And I am releasing the camera when destroying:
public void releaseCamera()
{
if (_camera != null)
{
_camera.stopPreview();
_camera.setPreviewCallback(null);
_camera.release();
_camera = null;
}
_surfaceHolder.removeCallback(this);
}
Can you think of any reason what-so-ever that I am getting this. I have a suspicion that theres some sort of bug because I am using camera and not camera2 but that is a wild guess. Reading the updates for API 6.0 there is a section on camera that says:
In This release, the model for accessing shared resources in the camera service has been changed from the previous “first come, first serve” access model to an access model where high-priority processes are favored.
Again without re-writing the entire app to use camera2 (not an option) I can't say for certain what's going on.
Here is my code where I open the camera (and what works on every other phone except the Nexus 5)
private void setInitialCamera()
{
try
{
if (_isBackFacing == true)
{
_camera = Camera.open(0);
} else
{
_camera = Camera.open(1);
}
} catch (RuntimeException e)
{
Log.d("Runtime Exception","Error " + e);
e.printStackTrace();
} catch (Exception e)
{
Log.d("Camera Error: ", " Android is unable tell what the error was");
e.printStackTrace();
}
}
Upvotes: 0
Views: 8642
Reputation: 2095
Making user to set permissions manually for your app is not a good approach. Use following code instead, which wil prompt user permission when your app is launched for first time.
First set your request code, which is used to recognize accepted or refused request:
private static final int MY_CAMERA_REQUEST_CODE = 100;
Then ask the user if you can use the camera:
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
else {
// permission has been already granted, you can use camera straight away
}
Finally check whether permissions were granted:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// user accepted your request, you can use camera now from here
}
else {
// user denied your request, you can now handle their decision
}
}
}
Upvotes: 0
Reputation: 13551
So it looks like the culprit has something to do with the 6.0.1 update this phone went through.
While this didn't happen on other phones, it did on the failing Nexus 5.
What happened was the 6.0.1 update allows users to set individual permissions for an app. So somehow the persmission for the camera were toggled off. Turning this back on fixed the issue.
To get there you go to Settings -> Apps -> [App Name] -> Permissions
Upvotes: 14