Flashlight (Torch) is opening but not closing

半城伤御伤魂 提交于 2019-12-23 05:27:34

问题


Following is the code which I used for opening Torch and closing it. But when I close it, it crashes. LogCat says " Runtime Exception : Fail to connect to camera service "!

+ hasFlash is not getting any value and is throwing Nullpointer exception. (I'm using it to check if the flash is present or not.)

What am I doing Wrong?

boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(hasFlash==true)

{

            if(s.equalsIgnoreCase("FlashLight On") || s.equalsIgnoreCase("Flash Light On"))
            {
                Camera cam = Camera.open();     
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                return "Turning on";

            }
            if(s.equalsIgnoreCase("FlashLight Off") || s.equalsIgnoreCase("Flash Light Off"))
            {

                Camera cam = Camera.open();
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                cam.release();

                return "Turning off";
            }
            }
            else
            {
                return "Flash Not Available";
            }

回答1:


Change

Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
cam.release();

return "Turning off";

to

cam.stopPreview();
cam.release();

return "Turning off";

I ran into a lot of these issues building an open source flashlight for Android which may help you with further questions.

Flashlight by Joe github



来源:https://stackoverflow.com/questions/23401891/flashlight-torch-is-opening-but-not-closing

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