I'm trying to develop a flashlight application for a local concert. This is part of a bigger application so it is in a fragment. This is the code:
First I declared the class together with its variables:
public class ConcertFragment extends Fragment {
ToggleButton btnFlashlight;
View rootView;
private Camera cam;
private boolean hasFlash;
boolean hasCamera;
boolean isFlashOn;
Parameters params;
public ConcertFragment() {
}
Next is the onActivityCreated
method which returns the getCamera method (declared further down):
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getCamera();
}
Then I create the onCreateView
method which builds the layout:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_concert, container, false);
hasFlash = getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(getActivity()).create();
alert.setTitle("No Flash");
alert.setMessage("Sorry, device is not flash supported.");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
}
btnFlashlight = (ToggleButton) rootView.findViewById(R.id.toggleButton);
btnFlashlight.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
return rootView;
}
Method to turn on the flash:
private void turnOnFlash() {
if (!isFlashOn) {
if (cam == null || params == null) {
return;
}
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(params);
cam.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
Method to turn off the flash:
private void turnOffFlash() {
if (isFlashOn) {
if (cam == null || params == null) {
return;
}
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
Method to set the camera parameters:
private void getCamera() {
if (cam != null) {
try {
cam = Camera.open();
params = cam.getParameters();
cam.startPreview();
hasCamera = true;
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
private void toggleButtonImage(){
if(isFlashOn){
btnFlashlight.setBackgroundResource(R.drawable.btn_switch_on);
}else{
btnFlashlight.setBackgroundResource(R.drawable.btn_switch_off);
}
}
}
}
The togglebutton switching on the flashlight switches on and off, but the flashlight never switches on at the back of my Nexus 5.
The related permissions I am using are as follows:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.VIBRATE" />
Can anyone help please?