Determine if a Camera Exists on a Device and the Number of Cameras

How to Determine if a Camera Exists on a Device (# of cameras)

For API Level 9+

1. Create an Android project, if you don't already have one.

2. Open the MainActivity.java file and add the below code to the imports section.

import android.hardware.Camera;

3. In the MainActivity, add the below code to the onCreate method.

int numCameras = Camera.getNumberOfCameras(); // .getNumberOfCameras Added in API level 9
if (numCameras > 0) {
Toast.makeText(this, "This device has " + numCameras + " cameras", Toast.LENGTH_LONG).show(); //needs import android.widget.Toast;
} else {
Toast.makeText(this, "A camera does NOT exist on this device", Toast.LENGTH_LONG).show(); //needs import android.widget.Toast;
}
}

4. Compile and run!