Wednesday, 24 August 2011

How to Turn On Flash Light/LED Flash In Android Mobile?

Hello,
Here i write simple application Demo that will Turn On Your Android Mobile Flash Light i have write this Demo For Android 2.2
Please Note That I am Not Sure Weather this Demo Will Work on All Android Mobile i have just Test This In Android HTC WildFire A3333 but Same Code Will Not Work On Samsung Galaxy Ace s-5830 So please Note this.

If Any One Find Good Solution to this Than You Can Share Your Answer .

Here is my Activity Class

public class FlashLightActivity extends Activity {

    /** Called when the activity is first created. */

private Button mOnBtn;
    private Button mOffBtn;
    private Camera mCamera;
    private boolean flash_ok=false;


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        flash_ok=getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        mOnBtn = (Button) findViewById(R.id.on_btn);
        mOnBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(mCamera==null){
mCamera=Camera.open();
}
if(flash_ok){
processOnClick();
}else{
Toast.makeText(FlashLightActivity.this, "No Flash Light", Toast.LENGTH_SHORT).show();
}
}
});
       
        mOffBtn = (Button) findViewById(R.id.off_btn);
        mOffBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
processOffClick();

}
});
    }
private void processOffClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_OFF );
            mCamera.setParameters( params );
            mCamera.release();
            mCamera = null;

        }
    }

private void processOnClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_TORCH );
            mCamera.setParameters( params );
        }
    }
@Override
protected void onResume() {
if(mCamera==null)
mCamera=Camera.open();
super.onResume();
}
@Override
protected void onDestroy() {
if(mCamera!=null){
mCamera.release();
}

super.onDestroy();
}
@Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
@Override
protected void onPause() {
if(mCamera!=null){
mCamera.release();
}
super.onPause();
}

}

-------------------------***************************------------------------------------------------
Here is My Manifest File You can see in that Which Permission are Require for Flash And Camera.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.flashlight"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
   
<uses-permission android:name="android.permission.CAMERA" />
  <uses-feature android:name="android.hardware.camera" />
  <uses-feature android:name="android.hardware.camera.autofocus" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FlashLightActivity"
                  android:label="@string/app_name" android:configChanges="orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>


-------------------------***************************------------------------------------------------

Below is My Xml Layout File.

Main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/on_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Flash ON" />

    <Button
        android:id="@+id/off_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Flash OFF" />
</LinearLayout>


if You Find Have Much Of Android Mobile not Work You can List That Here in Comment.

5 comments:

  1. Very nice tutorial!
    Would it be possible to get the eclipse project, to have a further look and test it? :)

    ReplyDelete
  2. Works on HTC Desire and Desire S.

    Although I think there's an error in your code:

    @Override
    protected void onResume() {
    if(mCamera==null)
    mCamera=Camera.open();
    super.onResume();
    }

    Should be:

    @Override
    protected void onResume() {
    if(mCamera!=null){
    mCamera=Camera.open();
    }
    super.onResume();
    }

    Is that right? Otherwise I get a force close.

    ReplyDelete
  3. not working in motorola atrix

    ReplyDelete
  4. On motorola atrix just add this line

    mCamera.startPreview();

    right after mCamera.setParameters( params );


    Track my ride android app

    ReplyDelete
  5. nice ..
    is it possible to change the brightness of LED ??

    ReplyDelete