How to detect camera availability in Windows Phone
This article explains how to detect camera availability in Windows Phone (and also briefly covers Android).
Detecting whether the camera is available on the device or not can be helpful in applications which require to launch camera. If we try to open camera without checking its existence, the app may crash. So, it is always better to determine in advance what kind of cameras (primary, secondary or both) are available on the device.
Detecting camera existence
To detect camera existence in device, we use PhotoCamera API. The PhotoCamera API provides a function IsCameraTypeSupported which accepts a string type variable stating the camera type.
The steps are:
- First we create an instance of PhotoCamera
private PhotoCamera _Camera;
- Next, we invoke the DETECT_CAMERA() method
public void DETECT_CAMERA()
if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
MessageBox.Show("Both Primary and Secondary Camera's are available.");
else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing))
MessageBox.Show("Only Secondary Camera is available.");
else if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
MessageBox.Show("Only Primary Camera is available.");
MessageBox.Show("No Camera available on this Device.");
Note: If you're migrating or porting from Android, then the equivalent class to determine whether the phone supports camera or not is PackageManager. PackageManager provides a function hasSystemFeature to determine whether a particular feature is available in the device or not. This function accepts a string type parameter, in this case FEATURE_CAMERA, specifying the feature whose information we need.
The code looks like this:
Boolean isAvailable = packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
You can download sample project code from this file Media:DetectingCamera.zip.