0

I have written an application that turns the flashlight on, on a mobile device. It only works on version(OS) 7.0 (Huawei), but it doesn't want to work on version 5.1.1(Samsung) & 4.42(LG G3).

On main activity load, i call : TurnCameraOff () -> this works

On Button on click, i call TurnCameraOn() -> this returs this error message :

Java.Lang.RuntimeException: Fail to connect to camera service at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c]

It breaks on this line : Camera camera = Camera.Open(); in TurnCameraOn() function.

I use these functions :

Turn camera on

 private void TurnCameraOn()
 {
    if (CameraAvailable() == true)
    {
                    Camera camera = Camera.Open();

                    Camera.Parameters parameters = camera.GetParameters();
                    parameters.FlashMode = Camera.Parameters.FlashModeTorch;

                    camera.SetParameters(parameters);
                    Android.Graphics.SurfaceTexture mPreviewTexture = new Android.Graphics.SurfaceTexture(0);

                    camera.SetPreviewTexture(mPreviewTexture);

                    camera.StartPreview();
      }
    }

Turn camera off

 private void TurnCameraOff()
 {
     if (CameraAvailable() == true)
                {
                    Camera camera = Camera.Open();

                    Camera.Parameters parameters = camera.GetParameters();

                    parameters.FlashMode = Camera.Parameters.FlashModeOff;

                    camera.SetParameters(parameters);
                    camera.StopPreview();

                    camera.Dispose();
                    parameters.Dispose();
                }
 }

Check if camera is available

  private bool CameraAvailable()
  {
            bool availale = true;

            if (PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureCamera) == false)
            {
                availale = false;
                Toast.MakeText(this, "No back-facing camera available", ToastLength.Long);
            }

            if (PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureCameraFlash) == false)
            {
                availale = false;

                Toast.MakeText(this, "No camera flash available", ToastLength.Long);
            }

            return availale;
     }

Manifest

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.flash" android:required="true" />
chosenOne Thabs
  • 1,480
  • 3
  • 21
  • 39
  • I presume you don't get those toast messages on the phones that don't work? Have you tried stepping through the code? – Neil Nov 24 '17 at 16:20
  • @Neil i have added more information to the question. Please check it out. – chosenOne Thabs Nov 24 '17 at 17:06
  • `Java.Lang.RuntimeException: Fail to connect to camera service` : Is a permission issue. – SushiHangover Nov 24 '17 at 20:01
  • @SushiHangover if i call TurnCameraOn() first the camera works. It gives me this error as soon as i call TurnCameraOff() and TurnCameraOn() again. – chosenOne Thabs Nov 25 '17 at 16:52
  • Declaring camera as a public variable solve the problem. This link solved y issue : https://stackoverflow.com/questions/23904459/android-java-lang-runtimeexception-fail-to-connect-to-camera-service – chosenOne Thabs Nov 25 '17 at 17:16

0 Answers0