1

Using the code found at How to turn on camera flash light programmatically in Android?, I tried to turn the flashlight on. The device I am using for testing is Galaxy SIII. The following is the code in my java file.

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

In the manifest file, the following permissions have been declared.

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />

However, when I try to turn the flashlight on from the app on my phone, the app crashes. I am guessing that this might be because the device itself doesn't support the flashlight remaining on. The logcat is as follows

05-17 07:52:30.489: E/AudioPolicyService(1902): getOutput() tid 17220 ++
05-17 07:52:30.489: E/AudioPolicyService(1902): getOutput() tid 17220 --
05-17 07:52:30.489: E/AudioPolicyService(1902): getOutput() tid 1902 ++
05-17 07:52:30.489: E/AudioPolicyService(1902): getOutput() tid 1902 --
05-17 07:52:30.489: E/AudioPolicyService(1902): getOutput() tid 2204 ++
05-17 07:52:30.489: E/AudioPolicyService(1902): getOutput() tid 2204 --
05-17 07:52:30.504: E/AudioMixer(1902): MOON > checkSEC_PCM ++ 1
05-17 07:52:30.504: E/AudioMixer(1902): MOON > checkSEC_PCM ++ 1
05-17 07:52:30.504: E/AudioResampler(1902): Unsupported sample format, 1 bits, 1 channels
05-17 07:52:30.619: E/AndroidRuntime(4740): FATAL EXCEPTION: main
05-17 07:52:30.619: E/AndroidRuntime(4740): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.app/com.example.app$Potter}: java.lang.RuntimeException: Fail to connect to camera service
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3182)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.ActivityThread.access$1100(ActivityThread.java:140)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.os.Looper.loop(Looper.java:137)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.ActivityThread.main(ActivityThread.java:4898)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at dalvik.system.NativeStart.main(Native Method)
05-17 07:52:30.619: E/AndroidRuntime(4740): Caused by: java.lang.RuntimeException: Fail to connect to camera service
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.hardware.Camera.native_setup(Native Method)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.hardware.Camera.<init>(Camera.java:348)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.hardware.Camera.open(Camera.java:325)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at com.example.app.MainActivity$Potter.startActivityWithName(MainActivity.java:100)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at com.example.app.MainActivity$Potter.onActivityResult(MainActivity.java:183)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.Activity.dispatchActivityResult(Activity.java:5390)
05-17 07:52:30.619: E/AndroidRuntime(4740):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
05-17 07:52:30.619: E/AndroidRuntime(4740):     ... 11 more
05-17 07:52:30.664: E/android.os.Debug(2271): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
05-17 07:52:38.849: E/MtpService(2094): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
05-17 07:52:38.849: E/MtpService(2094): battPlugged Type : 2
05-17 07:52:39.419: E/Launcher(22785): Error finding setting, default accessibility to not found: accessibility_enabled
05-17 07:52:44.159: E/Watchdog(2271): !@Sync 8677

What should I do in order to make this work or how can I first check if the device supports the flash turning on?

Community
  • 1
  • 1
Ankush
  • 6,767
  • 8
  • 30
  • 42

1 Answers1

0

The camera service acquisition is failing in your case. This can be due to many reasons. Another service might be using the camera and might not have released it. There might be no camera on the device (not in GS3) etc.

See these threads on more info for diagnosing your exact problem:

Failed to connect to camera service

Fail to connect to camera service

Android: Fail to connect to camera service at Camera.open();

Android - Fail to connect to camera

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Once I restarted my phone, the above code worked fine. Is it possible for me to prevent this from happening, i.e., if the app is about to crash, i'll let the user know that he needs to restart the phone – Ankush May 18 '13 at 19:02
  • You can catch the exception and show a toast to the user. Just put your `Camera.open()` in a try/catch block. – Anup Cowkur May 19 '13 at 05:13