How to turn ON and OFF Device flash light?

16,388

Solution 1

Bad news: According to documentation android camera needs intialized surface view to start preview Good news: it must not be full screen or visible

My expirience so far that starting preview is necessary to activate flash ( but I can not speak for all devices )

If you need some examples how to activate preview and possibly hide it behind overlay, look into javaocr library (see demos - there are 2 android apps, and also camera management library in separate project - you can just grab it)

https://sourceforge.net/projects/javaocr/

Solution 2

@RajaReddy PolamReddy,

I am using Samsung S3 LTE (4G). All you need change is to change Parameters.FLASH_MODE_ON to Parameters.FLASH_MODE_TORCH.

It works for me.

Solution 3

Try this

//Turn on
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

//Turn off
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
Share:
16,388

Related videos on Youtube

RajaReddy PolamReddy
Author by

RajaReddy PolamReddy

I love programming. I mostly code in "Android", "ActionScript", "Adobe Flash", Adobe AS3, iOS, Objective-C . My App.. Image Crop Current applications BingoBash and Slots Bash SOreadytohelp

Updated on June 04, 2022

Comments

  • RajaReddy PolamReddy
    RajaReddy PolamReddy about 2 years

    I want to turn on only camera flash light (not with camera preview) programmatically in Android. I googled for it but the help I found was not working on my Samsung Galaxy ACE.

    By using this code able to turn on for few seconds after that it goes to off automatically with out doing anything on the UI. If I try to turn on again getting force close.

    private void turnOn() {
    
        clicked = true;      
        mCamera = Camera.open();
        Parameters params = mCamera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_ON); 
        mCamera.setParameters(params);
        mCamera.startPreview();
        mCamera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) {
            }
        });
    }
    

    For turn OFF

    private void turnOff() { 
        clicked = false;         
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
        }
    }
    

    Declared these manifest permissions

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature android:name="android.hardware.camera.flash" />
    

    Can any body tell me How to turn on the flash light for a long time upto the use click on OFF button. Is it requires any third party libraries. I saw this app in the Google play Tiny flash how they did this app..

    From my after turn on it goes to off in 2 sec I did not get what's the problem in this.

    • RajaReddy PolamReddy
      RajaReddy PolamReddy over 11 years
      @Satya i saw that there but no use...
  • RajaReddy PolamReddy
    RajaReddy PolamReddy over 11 years
    how can i add preview to the code what i had posted in the Question. if you have anything related to this please post here..
  • user1475122
    user1475122 over 11 years
    Post your AndroidManifest.xml
  • user1475122
    user1475122 over 11 years
    try adding this just to test <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_ST‌​ATE"/>
  • user1475122
    user1475122 over 11 years
    What android version you have?
  • RajaReddy PolamReddy
    RajaReddy PolamReddy over 11 years
    i had created app on 2.2 my device 2.3.4