Android/Java: detect if device has a BACK Camera

For API >= 9 : you can use Camera.getCameraInfo with something like this :

int backCameraId = -1;
for(int i=0;i<Camera.getNumberOfCameras();i++){
    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(i,cameraInfo);
    if(cameraInfo.facing==CameraInfo.CAMERA_FACING_BACK) {
        backCameraId = i;
        break;
    }
}
Log.d(TAG, "back camera exists ? "+(backCameraId>-1));
Log.d(TAG, "back camera id  :"+backCameraId);

For API >= 21, you are advised to use the Camera2 API :

String backCameraId = null;
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
for(String cameraId:manager.getCameraIdList()){
    CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
    Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
    if(facing==CameraMetadata.LENS_FACING_BACK) {
        backCameraId = cameraId;
        break;
    }
}
Log.d(TAG, "back camera exists ? "+(backCameraId!=null));
Log.d(TAG, "back camera id  :"+backCameraId);

Try check the number of Cameras:

import android.hardware.Camera;

int numberOfCameras = Camera.getNumberOfCameras();

If it is greater than 1, pretty sure got a rear camera.


public boolean isRearCameraAvailable(Context context) {
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

Checks if the device has a camera facing away from the device user