title: Use camera flashlight in Android
body: <p>I'm trying to use the cameras LED flashlight in a widget. I've found several threads about this topic (i.e. <a href="https://stackoverflow.com/questions/5017455/how-to-use-camera-flash-led-as-torch-on-a-samsung-galaxy-tab">the one</a> mentioned later..) , now I'm trying to control the light using:</p>
  
  <pre><code>Camera cam = Camera.open();     
  Parameters p = cam.getParameters();
  p.setFlashMode(Parameters.FLASH_MODE_TORCH);
  cam.setParameters(p);
  cam.release();
  </code></pre>
  
  <p>In the AndroidManifest.xml tried different permissions, currently I have:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.FLASHLIGHT"/&gt;
  &lt;uses-feature android:name="android.hardware.camera" /&gt;
  &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;
  &lt;uses-feature android:name="android.hardware.camera.flash" /&gt;
  </code></pre>
  
  <p>I'm testing this on my Galaxy Tab as I don't have any other Android devices at hand: the light does not turn on. So I have a few questions now:</p>
  
  <ol>
  <li>Is there any way to test the led light behavior in the Emulator?</li>
  <li>Am I doing something wrong here?</li>
  <li>According to <a href="https://stackoverflow.com/questions/5017455/how-to-use-camera-flash-led-as-torch-on-a-samsung-galaxy-tab">this</a> question which deals with the same problem, it works differently on the Galaxy Tab. How?</li>
  <li>And finally, if it does work differently, I'm starting to wonder if it's just the Galaxy Tab or if other devices use different methods too. It would be hard to test then and it seems rather odd to me.</li>
  </ol>
  
  <p>Thanks for any insight!</p>
  
  <p>By the way, I quickly tested with <a href="http://code.google.com/p/quick-settings/" rel="nofollow noreferrer">quick-settings</a> which gets mentioned a few times here. The flashlight doesn't work with quick-settings either.</p>
  
  <p>Note that the Galaxy Tab stil uses android 2.2. I see there were some changes between 2.2 and 2.3.</p>
  
  <p>Comment:
  I know it has to work somehow as I have found other apps in the market that work perfectly with the Galaxy Tab.</p>
  
  <p>Comment 2: 
  If I set cam.setParameters(p); and directly ask the camera for the current state with getFlashMode() it correctly returns FLASH_MODE_TORCH. However, if I release the camera and re-open it, it returns FLASH_MODE_OFF. It's almost as if the Camera object aknowledges the request but doesn't really pass it on to the hardware!?</p>
  
  <p>--</p>
  
  <p>After Konstantins comment, I removed the cam.release(); part. He is right, the settings are not persisted if you release the camera. If you use cam.open() again, you will get a fresh instance with the light off. The light's still not working on the galaxy tab though.
  So, I guess it's hard to keep the light on if you're trying to control it through a widget then. As soon as the background service is finished, the camera object is released automatically and therefore the light switches off again. My questions still remain, especially why the camera doesn't switch on in the first place.</p>