Kumail Hussain Kumail Hussain - 18 days ago 6
Android Question

Android : Flash Light not working for lollipop

I am developing a Torch Application the problem I am facing is the methods work with kitkat and jelly bean but it is not working in some lollipop devices

Following is my MainActivity.java

public class MainActivity extends AppCompatActivity {
static Camera cam=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void On(View V) {
try {
if (getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
cam = Camera.open();
Camera.Parameters p = cam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),e.getMessage(), Toast.LENGTH_SHORT).show();
}

}

public void Off(View V) {
try {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
cam.stopPreview();
cam.release();
cam = null;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception flashLightOff", Toast.LENGTH_SHORT).show();
}
}
}


Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplication">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />


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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


I cannot find any method for lollipop , Any help will be appreciated .

Answer

Try following code:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends Activity {
    Camera mCamera;
    Camera.Parameters mParams;
    boolean on = false;
    int times;
    int delay;
    public static final String PREFS_NAME = "Torch";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b=(Button)findViewById(R.id.button);
        turnOff();
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                    Thread t = new Thread() {
                        public void run() {
                            try {
                                if (mCamera == null) {
                                    mCamera = Camera.open();
                                    try {
                                        mCamera.setPreviewDisplay(null);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    mCamera.startPreview();
                                }


                                    toggleFlashLight();


                                if (mCamera != null) {
                                    mCamera.stopPreview();
                                    mCamera.release();
                                    mCamera = null;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    };

                    t.start();
                }





        });


    }



    public void toggleFlashLight() {
        if (!on) {
            turnOn();
        } else {
            turnOff();
        }
    }
    public void turnOn() {
        if (mCamera != null) {
            mParams = mCamera.getParameters();
            mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(mParams);
            Toast.makeText(getApplicationContext(),"Flash ON",Toast.LENGTH_SHORT).show();

            on = true;
        }
    }
    public void turnOff() {
        if (mCamera != null) {
            mParams = mCamera.getParameters();
            if (mParams.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH)) {
                mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                mCamera.setParameters(mParams);
            }
        }
        on = false;
    }


}

you can turn off any time by calling Turnoff function

Don't forget to add permission in Manifest.xml

 <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
Comments