×
Namespaces

Variants
Actions

How to detect camera availability in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to detect camera availability in Windows Phone (and also briefly covers Android).

WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 7.1, Windows Phone 8.0
CompatibilityArticle
Created: Vaishali Rawat (19 Oct 2012)
Last edited: hamishwillee (09 Sep 2013)

Introduction

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.");
}
else
{
MessageBox.Show("No Camera available on this Device.");
}
}

Note.pngNote: 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);

Downloads

You can download sample project code from this file Media:DetectingCamera.zip.

This page was last modified on 9 September 2013, at 08:59.
69 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×