Android java lang runtimeexception cannot connect to camera service

I am currently working on turning on / off the flashlight. I get this error java.lang.RuntimeException: Fail to connect to camera service I do not know why this error occurs. I mentioned many solutions, but my problem is still not resolved. When the flashlight is on, an error does not occur, but when the flashlight is off, an error occurs.

My code The main code .

Resolution of my manifest:

 <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus"/> 
+45
android android-camera flashlight
May 28 '14 at 6:42
source share
12 answers

try it...

  static Camera camera = null; 

announce 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); } 
+16
May 28 '14 at 8:20
source share

I had the same problem that none of the answers here was resolved, so after solving it, I add my own way to solve it. This applies to newer versions of Android that support setting permissions for each application (starting with Marshmallow, 6.0). Camera permission can be disabled and must be enabled from application settings. Settings → Applications → [Your application] → Permissions

More on this here: http://developer.android.com/training/permissions/requesting.html

+81
Nov 19 '15 at 8:16
source share

I also saw this error:

java.lang.RuntimeException: Cannot connect to camera service

experimenting with a flashlight app. It turns out I was a bit messy with my permissions and copied them to the body of the application block in the manifest.xml file. So you really have to obey the syntax as described in:

http://developer.android.com/guide/topics/manifest/manifest-element.html

Otherwise, the application will fail if the connection to the service fails on a call to Camera.open (). It should look like this, based on your permissions in the question:

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

Make sure that your permission and the list of functions are contained only in the manifest section and are not buried in the application section!

+22
Nov 10 '14 at 11:25
source share

This problem can occur in android 6.0 if you have not allowed camera resolution for your application. Starting with Android 6.0, you can process permission to access applications that you grant or do not receive specific permission for the application.

So, you need to enable permission from settings->apps->your_app->enable camera permission , if it is not already enabled.

+8
Aug 24 '16 at 14:29
source share

If your version is os 6.0 or later try this, hope this helps.

 public class RequestUserPermission { private Activity activity; // Storage Permissions private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA }; public RequestUserPermission(Activity activity) { this.activity = activity; } public void verifyStoragePermissions() { // Check if we have write permission int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions( activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE ); } } } **//CALL FROM YOUR ACTIVITY** RequestUserPermission requestUserPermission = new RequestUserPermission(this); requestUserPermission.verifyStoragePermissions(); 
+7
Aug 18 '16 at 12:38 on
source share

if you try to open the camera using a camera ID that does not exist, you will get the same error (java.lang.RuntimeException: Fail to Connect to camera service)

look at your code on this block

 camera.setParameters(parameters); camera.stopPreview(); camera.release(); camera = null; 

Call release() to free the camera for use by other applications. Applications should immediately release the camera in onPause() (and reopen () in onResume().

In the above code, immediately after release, you put zero in the camera

+4
May 28 '14 at 7:26
source share

Hi, I hope that you are dealing with the flare view of the application or something related to flash memory, and there have been many discussions about this before, and here are some useful links and guides to achieve your needs, please go through them hope they can help you

How to enable camera flash in Android?

http://www.androidhive.info/2013/04/android-developing-flashlight-application/

http://www.compiletimeerror.com/2013/08/how-to-turn-onoff-camera-led-flashlight.html#.U4WH5Xbc3o4

http://android.programmerguru.com/android-flashlight-example/

+2
May 28 '14 at 6:59
source share

The simple answer I can find on this problem is that I did not ask for camera permission for the user, and therefore, by default, the camera resolution was not available to my application on Marshmallow devices. I just added a check for access to the camera before starting the camera, and everything works fine.

+1
Mar 25 '16 at 7:45
source share

You need stopPreview() and release() after you return from the camera, so that another application can access it. Make the Camera class static and name it null in onPause() . This solves my problem.

Try:

  public class CameraPhotoCapture extends Activity{ static Camera mcamera = null; @Override protected void onPause() { // TODO Auto-generated method stub if (mcamera != null) { mcamera.stopPreview(); mcamera.release(); mcamera = null; Log.d(DEBUG_TAG, "releaseCamera -- done"); } super.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub if (mcamera != null) { Camera.open(); Log.d(DEBUG_TAG, "openCamera -- done"); } super.onResume(); } } 
0
Sep 28 '15 at 14:29
source share

Try using this line when you exit the application:

 System.exit(0); 

I just got the Flashlight application code from somewhere. To close the application, System.exit(0) . I deleted it, knowing that this is not a good practice. Then I started getting these errors. I tried the solution to the accepted answer, but then I started getting some other errors. So instead of solving them, I just returned this line to System.exit(0) . And he began to work normally. I know this is not very good, but for a small flashlight application you can try this quick solution.

0
Nov 13 '15 at 0:24
source share

If all your code is OK, you should check if there is another application using your camera. Then you should close another application that is currently using your camera.

0
May 24 '16 at 11:55
source share

There is the same problem in my android project. This is my logcat error




03-29 19: 26: 04.194 224-608 /? V / EmulatedCamera_BaseCamera: getCameraInfo 03-29 19: 26: 04.196 224-224 /? I / CameraService: CameraService :: connect call (PID -1 "com.proitzen.staffapp", camera ID 1) for the default HAL version and Camera API version 1 03-29 19: 26: 04.196 224-224 /? W / ServiceManager: Permission error: android.permission.CAMERA from uid = 10067 pid = 1776 03-29 19: 26: 04.196 224-224 /? E / CameraService: Resolution: you cannot use the camera pid = 1776, uid = 10067 03-29 19: 26: 04.196 1776 -1 776 / com.proitzen.staffapp W / CameraBase: error 1 occurred while connecting to the camera: Service unavailable 03- 29 19: 26: 04.200 1776 -1 776 / com.proitzen.staffapp D / AndroidRuntime: shutting down the virtual machine




No solutions worked for me. My Android application worked on physical Android devices and gave the indicated error only in Genymotion.

Solution: run the settings of the Genumotion emulator → Applications ---> select the application → Permissions → turn on the camera, microphone and memory.

0
Mar 29 '18 at 23:39
source share



All Articles