简体   繁体   中英

RuntimeException with Samsung Galaxy — Flashlight

I have absolutely no idea how to implement this. It works fine on my Razr, but fails on a lot of Samsung devices. I have a SurfaceView running a camera preview. I've included a button to use the flashlight. The exception is thrown where I inserted the try block:

if (hasFlash()) {
        btnLight = (ImageButton) findViewById(R.id.btn_light);
        btnLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Parameters p = camera.getParameters();
                if (p.getSupportedFlashModes() != null) {
                    if (p.getSupportedFlashModes().contains(
                            Parameters.FLASH_MODE_TORCH)) {
                        if (isLightOn) {
                            p.setFlashMode(Parameters.FLASH_MODE_OFF);
                            camera.setParameters(p);
                            btnLight.setImageResource(R.drawable.light_on);
                            isLightOn = false;
                        } else {
                            try {
                                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                                camera.setParameters(p);
                                btnLight.setImageResource(R.drawable.light_off);
                                isLightOn = true;
                            } catch (RuntimeException e){
                                // I don't know how to make this stupid thing work for all phones
                            }
                        }
                    }
                }
            }
        });

In Android documentation you may read:

public void setParameters (Camera.Parameters params)

Throws
RuntimeException    if any parameter is invalid or not supported.

It means that you're using a camera parameter ( FLASH_MODE ) that is not supported by your Samsung device and therefore it won't work on this model. I assume that it probably just doesn't have flash light built-in at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM