为什么 parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_OFF return 不正确?
Why doesn't parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_OFF return true?
我正在尝试确定闪光灯是打开还是关闭,这样我就可以通过一个按钮将其打开和关闭。这是我的代码:
Camera cam = getCameraInstance(); //A method that opens the cam
Camera.Parameters parameters = cam.getParameters();
System.out.println("Current flash mode " + parameters.getFlashMode());
System.out.println("flash mode off equals to : " + Camera.Parameters.FLASH_MODE_OFF);
System.out.println(parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_OFF);
前两个 syso 的输出是 off
。自然地,我假设因为它们都是 return 一个 off
字符串,将 ==
运算符放在它们之间会 return true
(因此允许我检查闪光灯是否打开) 但它 returns false
。知道为什么吗?
我的目标是 api 16 并在 Marshmallow 设备上进行测试
如果勾选 docs you'll see that these parameters are String
s, and you don't compare strings in Java with ==
. You do that with equals
.
我正在尝试确定闪光灯是打开还是关闭,这样我就可以通过一个按钮将其打开和关闭。这是我的代码:
Camera cam = getCameraInstance(); //A method that opens the cam
Camera.Parameters parameters = cam.getParameters();
System.out.println("Current flash mode " + parameters.getFlashMode());
System.out.println("flash mode off equals to : " + Camera.Parameters.FLASH_MODE_OFF);
System.out.println(parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_OFF);
前两个 syso 的输出是 off
。自然地,我假设因为它们都是 return 一个 off
字符串,将 ==
运算符放在它们之间会 return true
(因此允许我检查闪光灯是否打开) 但它 returns false
。知道为什么吗?
我的目标是 api 16 并在 Marshmallow 设备上进行测试
如果勾选 docs you'll see that these parameters are String
s, and you don't compare strings in Java with ==
. You do that with equals
.