3

I saw that there are many questions liek this but I didn't find any way. In my app I have my own Camera with some options , and also have button named btnFlash for turning on/off camera's flash light while camera is running. I tried many ways, but nothing worked as on Samsung tab and HTC. here is one of them

ImageButton btnFlash = (ImageButton) findViewById(R.id.btn_flash);
        btnFlash.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Context context = getApplicationContext();
                if( context .getPackageManager().hasSystemFeature(getPackageManager().FEATURE_CAMERA_FLASH)) {
                    Parameters params = mCamera.getParameters();
                    if(isFlashOn) {
                        params.setFlashMode(Parameters.FLASH_MODE_OFF);
                        mCamera.setParameters(params);
                        isFlashOn = false;
                    } else {
                        params.setFlashMode(Parameters.FLASH_MODE_ON);
                        mCamera.setParameters(params);
                        isFlashOn = true;
                    }
                }
            }
        });
  1. Can anyone suggest some good tutorial or some code?
  2. Can same code work on e.g. HTC but not work on samsung? I mean can I wrote some code for any type of android device?

Thanks in advance..

divibisan
  • 11,659
  • 11
  • 40
  • 58
Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61
  • flash is having problems many time. You have to use surface view to on/off flash. Then it will work as charm. – baldguy Jan 21 '13 at 16:24
  • surface View? but how? I have one surface View , and on it is my camera's video, can you please tell me more details? thank you very much, and please leave your comment as an answer if you can give more details – Hayk Nahapetyan Jan 21 '13 at 16:43
  • Please see answer posted by me below. – baldguy Jan 21 '13 at 16:46

2 Answers2

2

Go through this link. This helps me to achieve flash functionality in almost all devices.

Import this SVN project.

http://code.google.com/p/torch/source/checkout

baldguy
  • 2,090
  • 1
  • 16
  • 25
  • @HaykNahapetyan Just go through this answer. – baldguy Jan 21 '13 at 16:46
  • @Rahil2952 please check this issue, same as this: http://stackoverflow.com/questions/21276908/flash-not-working-while-custom-camera-is-running – Sun Jan 22 '14 at 10:01
0

I had the same problem with you and I think you should replace

    params.setFlashMode(Parameters.FLASH_MODE_ON);

with this

    params.setFlashMode(Parameters.FLASH_MODE_TORCH);

I tested the param

    Parameters.FLASH_MODE_ON
    Parameters.FLASH_MODE_RED_EYE

and had the same problem, I think the problem occur because the device does not have appropriate hardware parametes