Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java Code of the Torch Flashlight App on the SSaurel's Blog
package com.ssaurel.mytorch;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button buttonEnable;
private ImageView imageFlashlight;
private static final int CAMERA_REQUEST = 50;
private boolean flashLightStatus = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageFlashlight = (ImageView) findViewById(R.id.imageFlashlight);
buttonEnable = (Button) findViewById(R.id.buttonEnable);
final boolean hasCameraFlash = getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
boolean isEnabled = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED;
buttonEnable.setEnabled(!isEnabled);
imageFlashlight.setEnabled(isEnabled);
buttonEnable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.CAMERA}, CAMERA_REQUEST);
}
});
imageFlashlight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (hasCameraFlash) {
if (flashLightStatus)
flashLightOff();
else
flashLightOn();
} else {
Toast.makeText(MainActivity.this, "No flash available on your device",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void flashLightOn() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true;
imageFlashlight.setImageResource(R.drawable.btn_switch_on);
} catch (CameraAccessException e) {
}
}
private void flashLightOff() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, false);
flashLightStatus = false;
imageFlashlight.setImageResource(R.drawable.btn_switch_off);
} catch (CameraAccessException e) {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode) {
case CAMERA_REQUEST :
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
buttonEnable.setEnabled(false);
buttonEnable.setText("Camera Enabled");
imageFlashlight.setEnabled(true);
} else {
Toast.makeText(MainActivity.this, "Permission Denied for the Camera", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment