1
使用在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): [email protected] > 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): [email protected] 8677
我應該爲了使這項工作或我怎麼能先檢查設備是否支持閃光燈打開呢?
一旦我重新啓動手機,上面的代碼工作正常。我是否有可能防止這種情況發生,即如果應用程序即將崩潰,我會讓用戶知道他需要重新啓動手機 – Ankush
您可以捕獲異常並向用戶顯示敬酒。只需將你的Camera.open()放在try/catch塊中即可。 –