Android手电筒代码不起作用 [英] Android flashlight code not working

查看:245
本文介绍了Android手电筒代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的项目以使用手电筒功能.在许多不同的地方(基本上都以相同的方式来完成),我汇编了以下代码,但手电筒无法打开(不引发任何异常)-但是在以下情况下它可以与相机配合使用我拍张照片:

I created a simple project to play with flashlight feature. From many different places (which all do it basically the same way), I have assembled the following code, but the flashlight will not turn on (No exceptions are raised) - but it works with the camera when I take a photo:

示例代码

示例代码2

示例代码3

    @Override
    protected void onResume() {
        super.onResume();

        try {


            // check flashlight support
            hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

            if (!hasFlash) {
                // device doesn't support flash
                // Show alert message and close the application
                AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
                alert.setTitle("Error");
                alert.setMessage("Sorry, your device doesn't support flash light!");
                alert.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        finish();
                    }
                });
                alert.show();
                return;
            }

            text_off = (TextView) findViewById(R.id.text_off);
            text_off.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                    return;
                }
            });


            getCamera();
            turnOnFlash();

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "onResume Error: "+e.getMessage());
        }

    }

    @Override
    protected void onPause() {      
        super.onPause();

        turnOffFlash();

    }






    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                Log.e(this.getClass().getSimpleName(), "Camera Error. Failed to Open. Error: "+e.getMessage());
            }
        }   
    }

    @SuppressWarnings("deprecation")
    private void turnOnFlash() {

        try {
            Log.d(this.getClass().getSimpleName(), "turnOnFlash CHECKPOINT ");


            if (camera == null || params == null) {
                return;
            }


            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_ON); 
            // ALSO TRIED: params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;

            Log.d(this.getClass().getSimpleName(), "turnOnFlash CHECKPOINT EXIT");

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "Camera Error. Failed to Open. Error: "+e.getMessage());
        }

    }


    @SuppressWarnings("deprecation")
    private void turnOffFlash() {

        try {

            Log.d(this.getClass().getSimpleName(), "turnOffFlash CHECKPOINT ");

            if (camera == null || params == null) {
                return;
            }

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            camera.release();
            isFlashOn = false;

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "Camera Error. Failed to Open. Error: "+e.getMessage());
        }

    }

清单中

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

此代码为什么不打开/关闭手电筒?

Why doesn't this code turn the flashlight on/off?

硬件:Nexus 5

Hardware: Nexus 5

OS:Android棉花糖

OS: Android Marshmallow

推荐答案

我认为您缺少一些设备需要的另一行代码.这三行是到目前为止我在所有设备上需要的内容:

I think you are missing one additional line of code, which is needed for some devices. These 3 lines are whats needed on all devices I encountered so far:

// will work on some devices
mCamera.setParameters(parameters);
// Needed for some devices.
mCamera.setPreviewTexture(new SurfaceTexture(0));
// Needed for some more devices.
mCamera.startPreview();

如果添加一些虚拟SurfaceTexture,它将正常工作.您还可以看到完整的代码示例此处.

If you add some dummy SurfaceTexture it should work. Also you can see a full code sample here.

不同手机之间的相机api差异很大,而这些电话的详细信息记录得很少.

The camera api varies highly between phones and those phone details are poorly documented.

这篇关于Android手电筒代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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