Setting Parameters.FLASH_MODE_TORCH doesn't work on Droid X 2.3

时光总嘲笑我的痴心妄想 提交于 2019-12-04 11:22:34

There are quite a few quirks when setting FLASH_MODE_TORCH.

Often you need to start a camera preview:

Camera mCamera = Camera.open();
mCamera.startPreview();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);

That may resolve it on some phones, other phones also require the preview to be drawn to a SurfaceView. This can be done by implementing SurfaceHolder.Callback interface in your activity. See an example here.

It could be that the Droid X doesn't support Torch Mode. Try something like this:

        List<String> pList = camera.getParameters().getSupportedFlashModes();

        if (pList.contains(Parameters.FLASH_MODE_TORCH))
            parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);


         camera.setParameters(parameters);

Refer to Issue 191453:

SurfaceTexture mDummy = new SurfaceTexture(1); // any int argument will do
camera.setPreviewTexture(mDummy);
camera.startPreview();
Rick Stranberg

The only thing I found that works on the Droid X is the code presented by Siddhpura Amit part way down the page in this answer Use camera flashlight in Android. He checks the manufacturer and checks to see if it contains the string "motorola." If so, he has special code that can switch the camera Flash LED on or off. I can verify that it does work as I have a Motorola Droid X.

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