LED Flashlight does not work on Samsung Galaxy Nexus

。_饼干妹妹 提交于 2019-11-29 06:54:23

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!