This is just some simple inline Java code to check if a device has a Back facing camera, a Front facing camera, and a Flash (it requires the JavaObject library to be enabled).
B4X:
#Region Project Attributes
#ApplicationLabel: HasFrontBackCameraAndFlash
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim nativeMe As JavaObject
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As Button
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
nativeMe.InitializeContext
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
'if the device has a back camera only then the ID's will be BACK = 0 and FRONT = -1
'if the device has a front camera only then the ID's will be BACK = -1 and FRONT = 0
'if the device has a back and front camera then the ID's will be BACK = 0 and FRONT = 1
Log("ID of Front Facing Camera = " & nativeMe.RunMethod("getFrontFacingCamera", Null))
Log("ID of Back Facing Camera = " & nativeMe.RunMethod("getBackFacingCamera", Null))
Log("Device has a flash = " & nativeMe.RunMethod("hasFlash", Null))
Label1.Text = " ID of Front Facing Camera = " & nativeMe.RunMethod("getFrontFacingCamera", Null) & Chr(10) & _
" ID of Back Facing Camera = " & nativeMe.RunMethod("getBackFacingCamera", Null) & Chr(10) & _
" Device has a flash = " & nativeMe.RunMethod("hasFlash", Null)
End Sub
#If Java
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.CameraInfo;
import android.content.pm.PackageManager;
import android.content.Context;
public int CAMERA_FRONT = -1;
public int CAMERA_BACK = -1;
private boolean hasflash = false;
public int getFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
// get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
// for every camera check
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
break;
}
}
CAMERA_FRONT = cameraId;
return cameraId;
}
/**
* Get the ID of the back facing camera
* If the device has a front and back camera then the ID = 0
* If the device has a back facing camera only then the ID = 0
* It will return the ID as -1 if there is no back facing camera
*/
public int getBackFacingCamera() {
int cameraId = -1;
// Search for the back facing camera
// get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
// for every camera check
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
break;
}
}
CAMERA_BACK = cameraId;
return cameraId;
}
public boolean hasFlash() {
/*
* First check if device is supporting a flashlight or not
*/
hasflash = BA.applicationContext.getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
return hasflash;
}
#End If