1

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
                            }
                        }
                    }
                }
            }
        });
a person
  • 986
  • 6
  • 13
  • Usually I'll print out all camera parameters and check what values I can use. The hardware APIs can not work for all devices. – yushulx Feb 13 '14 at 02:31

1 Answers1

2

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.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
  • I don't think so because it has to go through several checks. First the method hasFlash which checks getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH). Then the getSupportedFlashModes check, then I check to see if flash modes contains FLASH_MODE_TORCH – a person Feb 12 '14 at 13:13
  • How can it pass all those checks? – a person Feb 12 '14 at 13:14
  • @user3029413 Ok, what are the details of this `RuntimeException`? Do you have any? – Piotr Chojnacki Feb 12 '14 at 13:23
  • java.lang.RuntimeException: setParameters failed at android.hardware.Camera.native_setParameters(Native Method) at android.hardware.Camera.setParameters(Camera.java:1352) at – a person Feb 12 '14 at 13:27
  • I have been trying to find documentation on Samsung's site but I can't find anything. – a person Feb 12 '14 at 13:30
  • @user3029413 You should probably find your answer here: http://stackoverflow.com/questions/3890381/camera-setparameters-failed-in-android – Piotr Chojnacki Feb 12 '14 at 13:30
  • I changed it to select a size from the list rather than trying to get the best result myself. It's strange that the RuntimeException was flagged at the flash code, not in onSurfaceChanged() – a person Feb 12 '14 at 14:05
  • @user3029413 Unfortunately sometimes we have to look for problem deeper than logs tell us. Anyway, I'm glad you solved your issue. – Piotr Chojnacki Feb 12 '14 at 14:08