I've got the following problem: My Flashlight app works fine on my Samsung Galaxy S2 but unfortunately not on the Samsung Galaxy Nexus (problem: flashlight ignores the button-click -> no reaction, no light, no crash, no exception). I've read "LED flashlight on Galaxy Nexus controllable by what API?" here in stackoverflow but it did not help me since my problem still occures. This is my code-snippet to control the light:
final Button FlashLightControl = (Button)findViewById(R.id.ledbutton);
FlashLightControl.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg)
{
if(camera != null)
{
//in case light is on we will turn it off
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
camera.release();
camera = null;
}
else
{
// light is off - we turn it on
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
}});
Any ideas? For the sake of completeness - these are the permissions I added to the Androidmanifest.xml:
<uses-feature android:name="android.hardware.camera.flash" />
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Can someone help?
Kind regards, CarpeTemporem
I also had the same problem, but I was trying to turn on the LED from a Service, so I couldn't use a 1x1 SurfaceView. Here's what I did to make it work.
SurfaceTexture was added in API Level 11 (Android 3.0) so it will only work on Honeycomb or newer. For older API levels you can stick with the SurfaceView trick in the other answer.
I had the same problem and solved it via using Surface View having 1px width and 1px height