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 had the same problem and solved it via using Surface View having 1px width and 1px height
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.
private void turnLEDOn() throws IOException
{
// In order to work, the camera needs a surface to turn on.
// Here I pass it a dummy Surface Texture to make it happy.
camera = Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
}
private void turnLEDOff()
{
if (camera != null)
{
// Stopping the camera is enough to turn off the LED
camera.stopPreview();
camera.release();
camera = null;
} else
throw new NullPointerException("Camera doesn't exist to turn off.");
}
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.
来源:https://stackoverflow.com/questions/9505945/led-flashlight-does-not-work-on-samsung-galaxy-nexus