0

I have a camera layout. I need that to handle the flash icon. Just like in the normal in-built cameras, you click on the icon, the icon and the flash mode changes. The icon is in the action bar.

    final ImageView flashicon=(ImageView)view.findViewById(R.id.flash_actionbar);
    flashicon.setOnClickListener(new View.OnClickListener() {
        Drawable cameraflashautoicon=getResources().getDrawable(R.drawable.cameraflashauto);
        @Override
        public void onClick(View v) {
        if(flashicon.getDrawable().getConstantState().equals(R.drawable.cameraflashauto)){
                flashicon.setImageDrawable(cameraflashautoicon);
            }
            if(flashicon.getDrawable().getConstantState().equals(R.drawable.cameraflashoff)){

            }
            if(flashicon.getDrawable().getConstantState().equals(R.drawable.cameraflashon)){

            }
        }
    });

I read a few posts related to this, but couldn't figure out what to do. Please help. The way this question is proposed may not be ideal, so please let me know what other code or detail is needed.

I am using surface view and then calling the camera instance.

VC.One
  • 14,790
  • 4
  • 25
  • 57
Harshita
  • 382
  • 1
  • 6
  • 21

1 Answers1

0

For this you should do like :

Check whether flash light is available or not ? If yes then Turn Off/On If no then you can do whatever according to your app. needs For Checking availability of flash in device:

You can use the following

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

which will return true if a flash is available, false if not.

See http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.

Then use below code for flash light handling

CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.

camManager.setTorchMode(cameraId, true);