0

I'm building a flashlight app and I've learned that when I call

this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH),

the Nexus 7 returns true and when asking for the FlashModes() the size is 1 and the option is called "off." So when I check whether or not flash exists on devices, if the device lies about it, I can run into trouble. I've added !android.os.Build.MODEL.equals("Nexus 7") to my if statement to prevent crashing my app while testing on my Nexus 7.

If there are other devices that lie about having a flash, my app will likely crash on them. How can I avoid this?

gaurav5430
  • 12,934
  • 6
  • 54
  • 111
Zeek Aran
  • 523
  • 4
  • 18

1 Answers1

1

you can implement a double check

after calling

this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)

check if

getSupportedFlashModes() returns null

if no

check if

flash modes only contains one entry  "off"
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • Will every device that doesn't have a flash but hasSystemFeature returns true have a flash mode of "off" while any device that DOES have a flash will not have a mode called "off"? – Zeek Aran Dec 23 '13 at 17:22
  • @ZeekAran edited the answer, check for length odf supported flash modes and value=off – gaurav5430 Dec 23 '13 at 17:24
  • @ZeekAran the only cases i know off, is either getSupportedFlashModes() return null (e.g. for some chinese phones) or have a single entry "off" (like for nexus 7). – gaurav5430 Dec 23 '13 at 17:25
  • Excellent, thank you. I am now using: if (!hasSystemFeature) else p = cam.getParameters(); if (p.getSupportedFlashModes() != null) { if (p.getSupportedFlashModes().size() == 1) { if (p.getSupportedFlashModes().get(0).equals("off")) { And this properly checks my deceitful Nexus 7. – Zeek Aran Dec 23 '13 at 18:03