手电筒(火炬)正在打开,但不打烊 [英] Flashlight (Torch) is opening but not closing

查看:134
本文介绍了手电筒(火炬)正在打开,但不打烊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是code这是我用于打开火炬和关闭它。但是,当我关闭它,它崩溃。 LogCat中说!运行时异常:无法连接到相机服务

+
hasFlash没有得到任何值,并抛出空指针异常。 (我用它来检查闪光灯present与否。)

什么我做错了?

 布尔hasFlash = this.getPackageManager()hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)。
如果(hasFlash ==真){            如果(s.equalsIgnoreCase(手电筒)|| s.equalsIgnoreCase(闪光灯开))
            {
                相机凸轮= Camera.open();
                参数P = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(P);
                返回打开;            }
            如果(s.equalsIgnoreCase(手电筒关)|| s.equalsIgnoreCase(闪光灯关))
            {                相机凸轮= Camera.open();
                参数P = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(P);
                cam.stop preVIEW();
                cam.release();                返回关闭;
            }
            }
            其他
            {
                回归不能使用闪光灯;
            }


解决方案

修改

 相机Cam = Camera.open();
参数P = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(P);
cam.stop preVIEW();
cam.release();返回关闭;

  cam.stop preVIEW();
cam.release();返回关闭;

我遇到了很多这些问题建立一个开源的手电筒为Android可以帮助你进一步的问题。

乔手电筒github上

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";
            }

解决方案

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

这篇关于手电筒(火炬)正在打开,但不打烊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆