Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Open FlashLight of Android Phone Programmatically

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 236
    Comment on it

    How to open flashLight of android phone programmatically

    declare these variables in the activity

     private boolean isLighOn = false; // to check tht flash is on or off
     boolean flashAvailable=false;
      int cameraID=0  /*cameraId with 0 represents back camera**/
    
    
    

    firstly we have to check if phone supports flash light or not.

     Camera myCamera=getCameraInstance(cameraID);
      final Parameters parameter = myCamera.getParameters();
    
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
         {
          flashAvailable=true;
         }
     else
         {
           flashAvailable=false;
         }
    

    code snippet to on-off the falsh

        Button button = (Button) findViewById(R.id.buttonFlashlight); 
    button.setOnClickListener(new OnClickListener() {
     @Override public void onClick(View arg0) { 
    if(flashAvailable){
     if (isLighOn) 
       {
         p.setFlashMode(Parameters.FLASH_MODE_OFF);
         try {
              myCamera.setParameters(p); 
               }
          catch(RuntimeException ex) 
             {
              } 
          myCamera.startPreview(); 
          isLighOn = false; 
        } 
      else
      {
         p.setFlashMode(Parameters.FLASH_MODE_TORCH);
           try { 
               myCamera.setParameters(p); 
               } catch(RuntimeException ex) 
              {
               } myCamera.startPreview();
           isLighOn = true; 
         } 
      } 
    else
     { 
    Toast.makeText(getApplicationContext(),"Your Phone does not have flash", Toast.LENGTH_SHORT).show();
     } 
    } 
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: