Difficult to connect to the camera service

Welcome to ScriptMode
Rami
Posts: 36
Joined: Tue Aug 25, 2015 9:39 am

Difficult to connect to the camera service

Postby Rami » Wed Mar 02, 2016 7:14 am

I'm trying to access the camera on my phone. I'm writing a simple stub app prior to putting the code in a widget. I'm not getting very far. The code always throws a runtime exception "failed to connect to camera service" The code(pinched from a commonsware example) which goes wrong is:

@Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
Camera.CameraInfo info = new Camera.CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
try {
// Gets to here OK
camera = Camera.open(i);
} catch (Exception e) {
e.printStackTrace();
// throws runtime exception :"Failed to connect to camera service"
}
}
}
}
}
and my manifest is (corrected 20th Oct):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nbt.cameratest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".CameraTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</application>
</manifest>
Can anyone please suggest what might be wrong?

UPDATE 20th Oct

Logcat in SDK 4.0 is broken and wont show the end of the log, so I've cut this bit as best as I can from command line adb logcat:

W/ServiceManager( 2588): Permission failure: android.permission.CAMERA from uid=10136 pid=5744
E/CameraService( 2588): Permission Denial: can't use the camera pid=5744, uid=10136
W/System.err( 5744): java.lang.RuntimeException: Fail to connect to camera service
W/System.err( 5744): at android.hardware.Camera.native_setup(Native Method)
W/System.err( 5744): at android.hardware.Camera.<init>(Camera.java:294)
W/System.err( 5744): at android.hardware.Camera.open(Camera.java:255)
etc..

Paul
Posts: 137
Joined: Mon Aug 24, 2015 4:53 pm

Re: Difficult to connect to the camera service

Postby Paul » Fri Mar 04, 2016 1:37 pm

Few things:

Why are your use-permissions and use-features tags in your activity tag. Generally, permissions are included as direct children of your <manifest> tag. This could be part of the problem.

According to the android camera open documentation, a runtime exception is thrown:

if connection to the camera service fails (for example, if the camera is in use by another process or device policy manager has disabled the camera)
Have you tried checking if the camera is being used by something else or if your policy manager has some setting where the camera is turned off?

Don't forget the <uses-feature android:name="android.hardware.camera.autofocus" /> for autofocus.

While I'm not sure if any of these will directly help you, I think they're worth investigating if for no other reason than to simply rule out. Due diligence if you will.

EDIT As mentioned in the comments below, the solution was to move the uses-permissions up to above the application tag.


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest