android java lang runtimeexception fail to connect to camera service
try this...
static Camera camera = null;
declare it on top.
try{
if(clickOn == true) {
clickOn = false;
camera = Camera.open();
Parameters parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
remoteViews.setViewVisibility(R.id.button1, View.GONE);
remoteViews.setViewVisibility(R.id.button2, View.VISIBLE);
localAppWidgetManager.updateAppWidget(componentName, remoteViews);
} else {
clickOn = true;
camera.stopPreview();
camera.release();
camera = null;
remoteViews.setViewVisibility(R.id.button1, View.VISIBLE);
remoteViews.setViewVisibility(R.id.button2, View.GONE);
localAppWidgetManager.updateAppWidget(componentName, remoteViews);
}
} catch(Exception e) {
Log.e("Error", ""+e);
}
How to fix Fail to connect to camera service exception in Android emulator
From the Android Developers Docs:
Calling Camera.open() throws an exception if the camera is already in use by another application, so we wrap it in a try block.
Try wrapping that code in a try catch block like so:
try {
releaseCameraAndPreview();
if (camId == 0) {
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
}
else {
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
}
} catch (Exception e) {
Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}
Then add this function somewhere:
private void releaseCameraAndPreview() {
myCameraPreview.setCamera(null);
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
ERROR: failed to connect to camera service @ Android marshmallow
You need to give android.hardware.Camera
permission programmatically.
MANIFEST PERMISSIONS WON'T WORK on Android 6
With marshmallow(newest version of Android). We have got some
restrictions in Using Sensitive permissions like :
Storage,Contacts access, etc..In edition to give these permissions in
manifest, We need to request them from users at Runtime.
For more details refer this : Android M permissions
Add this code in your activity :
@Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
List<String> permissions = new ArrayList<String>();
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.CAMERA);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
}
}
}
Add this after onActivityResult
:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 111: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
} else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
}
}
}
break;
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
Failed to connect to camera service
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.
Xamarin Forms Java.Lang.RuntimeException: Fail to connect to camera service
Just make a conclusion of the comments:
You need to request permissions
at runtime when you want to use camera in Android project.
To request permissions, you can use Xamarin.Essentials: Permissions:
var status = await Permissions.RequestAsync<Permissions.Camera>();
There are also many other permission request information in the document.
Related Topics
What's Oncreate(Bundle Savedinstancestate)
Custom Info Window Adapter with Custom Data in Map V2
Android: Change Shape Color in Runtime
How to Make Notification Intent Resume Rather Than Making a New Intent
What Does the Layoutinflater Attachtoroot Parameter Mean
Implementing User Choice of Theme
Download Files and Store Them Locally with Phonegap/Jquery Mobile Android and iOS Apps
Error Importing Holoeverywhere
"Realm Migration Needed", Exception in Android While Retrieving Values from Realm Db
How to Add Buttons at Top of Map Fragment API V2 Layout
How to Make Circle Custom Progress Bar in Android
Android Remove Space Between Tabs in Tabwidget
Android: Implementing Progressbar and "Loading..." for Endless List Like Android Market
How to Get Available Wifi Networks and Display Them in a List in Android
Add Shadow to Custom Shape on Android