问题
I am writing an app that sets the flash mode to torch. I have been testing the application on my Droid X, and the LED light does not come on. I tried it on a Droid Incredible and it worked fine. I can't figure out what the problem is. Here is part of my code for turning on torch mode.
Camera mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
I have added mCamera.startPreview(); because I read that should make a difference, but it doesn't. I also made a list of available flash modes and displayed them to the screen to make sure that my Droid X does have torch mode, and it was in the list. I even created a new application from code I found online that turns the LED flash on and off with a button. Again it worked fine on the Droid Incredible but not the Droid X. Is there something I am missing to get this to run on the Droid X, or could it be something with Gingerbread? The Droid X is running Gingerbread and the Droid Incredible is running FroYo.
回答1:
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.
回答2:
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);
回答3:
Refer to Issue 191453:
SurfaceTexture mDummy = new SurfaceTexture(1); // any int argument will do
camera.setPreviewTexture(mDummy);
camera.startPreview();
回答4:
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.
来源:https://stackoverflow.com/questions/6881004/setting-parameters-flash-mode-torch-doesnt-work-on-droid-x-2-3