Hi, I am programming in android studio and I need to know how to detect if an android device has a camera with flash and then launch a Toast informing if it has a camera or not with flash.
Hi, I am programming in android studio and I need to know how to detect if an android device has a camera with flash and then launch a Toast informing if it has a camera or not with flash.
You can do it by:
getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
What you do is check the system features if the camera supports the flash CAMERA_FLASH
This would be the Toast
:
Toast.makeText(getApplicationContext(), getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)?"Camara tiene Flash":"Camara no tiene flash",Toast.LENGTH_SHORT).show();
To check flash availability on a device:
Use:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
Returns true if the flash is available, false if not.
Very good. You can check if you have flash as follows:
boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
If this code does not work for you, you can try this one:
Camera camera; // Inicializas la cámara
public boolean hasFlash() {
if (camera == null) {
return false;
}
Camera.Parameters parameters = camera.getParameters();
if (parameters.getFlashMode() == null) {
return false;
}
List<String> supportedFlashModes = parameters.getSupportedFlashModes();
if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
return false;
}
return true;
}
You can see the source here: link
First of all, remember that most devices have more than one camera.
What you should do is first get a list of available cameras and then search among them which one has a flash, if any has a flash then you return a 'True'.
The correct way is to do that from the Google Camera2 API.
Try this code:
public boolean hasFlash() {
boolean hasFlash = false;
//Primero obtienes el servicio de camara.
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
try {
//Obtienes todos los ID'S de las camaras disponibles
//Por lo regular son dos (Camara trasera, Camara frontal)
String[] cameraIds = cameraManager.getCameraIdList();
//Buscas entre todas ellos si esque alguna tiene flash.
for (String cameraId :
cameraIds) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
if (characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE).booleanValue())
hasFlash = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasFlash;
}