How to turn ON and OFF Device flash light?
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();
Related videos on Youtube
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, 2022Comments
-
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 over 11 years@Satya i saw that there but no use...
-
-
RajaReddy PolamReddy over 11 yearshow 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 over 11 yearsPost your AndroidManifest.xml
-
user1475122 over 11 yearstry adding this just to test <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
-
user1475122 over 11 yearsWhat android version you have?
-
RajaReddy PolamReddy over 11 yearsi had created app on 2.2 my device 2.3.4