打开闪光灯时,应用程序崩溃

时间:2013-05-17 02:47:26

标签: android android-camera

使用How to turn on camera flash light programmatically in Android?处的代码,我试图打开手电筒。我用于测试的设备是Galaxy SIII。以下是我的java文件中的代码。

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

在清单文件中,声明了以下权限。

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

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

然而,当我尝试从手机上的应用程序打开手电筒时,应用程序崩溃了。我猜这可能是因为设备本身并不支持手电筒继续使用。 logcat如下

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

如何使这项工作或我如何首先检查设备是否支持闪光灯开启?

1 个答案:

答案 0 :(得分:0)

在您的情况下,相机服务获取失败。这可能是由于许多原因造成的。另一项服务可能是使用相机,可能没有发布它。设备上可能没有摄像头(不在GS3中)等。

有关诊断确切问题的详细信息,请参阅以下主题:

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