1

There is a need to use the built in device camera flash as a torch. Unfortunately I don't have access to an Android device so I can only use the Android emulator. How can I validate that my code does work?

I have been trying several times to update my app with different ways to access the camera flash and each time the app was crashed.

My app minimum OS version is 2.2,

At the moment I'm using the following code


private void turnFlashOn() {
        camera = Camera.open();
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        camera.startPreview();
    }

    private void turnFlashOff() {
        camera = Camera.open();
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
    }

Haven't tested it yet.

What is your way to use the camera flash?

Thanks!

4

2 回答 2

1

这里的前两个错误是你已经调用Camera.open()了两次并且从未关闭它。您需要在其中打开它并在onResume()其中关闭它onPause(),并在两者之间操作Camera您获得的手柄。

于 2012-11-07T09:22:30.270 回答
0

您可以在模拟器和模拟相机类(使用 jmockit)上进行调试,但您必须小心 - 并非真实设备上的每个相机都支持手电筒模式(或根本没有闪光灯) - 所以您必须检查相机对象是否支持这个。

一旦您的代码正确运行,您仍然需要在真实设备上对其进行测试(提示:如果您有礼貌地询问并在网络上的某处提供签名的 APK 以便人们可以安装它,您可以在 android 开发人员邮件列表中招募测试人员)

于 2012-10-30T08:42:45.070 回答