Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Determining the number of cameras in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia N73, Nokia E7-00
Platform(s): S60 3rd Edition FP2, Nokia Belle
Nokia Belle
Java Runtime 2.3 for Symbian
S60 3rd Edition FP2
Created: dekudin (28 Nov 2008)
Last edited: hamishwillee (29 Jul 2013)



This code snippet demonstrates how to determine the number of cameras presented on device.

To retrieve the number of cameras this snippet creates player for each camera. If the player for any camera could not be created, this means that there is no such camera on the phone. So the snippet creates a player for camera devcam0, then (if it was successful), for camera devcam1 and so on until an exception is received because a player for camera devcamN could not be created, meaning that this camera does not exist. So number of cameras = N-1.

Source file:

import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.CommandListener;
public class NumberOfCameras extends MIDlet implements CommandListener {
private Display display;
// Form where number of cameras is placed.
private Form form;
// Command for exiting from midlet.
private Command cmdExit;
* Constructor

public NumberOfCameras() {
* Initializes components of midlet

private void initializeComponents() {
// Get display
display = Display.getDisplay(this);
// Create form
form = new Form("Cameras avaliable");
cmdExit = new Command("Exit", Command.EXIT, 0);
// Output number of cameras
int numCameras = getNumberOfCameras();
form.append(new StringItem("Number of cameras", Integer.toString(numCameras)));
* Gets number of cameras on the phone.
* To retrieve number of cameras this method creates player for each camera.
* If player for some camera can not be created this means that there is
* no such camera on the phone and method already created players for all
* cameras.
* @return number of cameras.

private int getNumberOfCameras() {
int camIndex = 0;
try {
for( ; ; ++camIndex) {
String locator = "capture://devcam" + Integer.toString(camIndex);
Player player = Manager.createPlayer(locator);
catch(MediaException mediaExc) {
// This exception is thrown if player can not be created
// from locator.
return camIndex;
catch(Exception exc) {
return 0;
* From MIDlet.
* Signals the MIDlet that it has entered the Active state.

public void startApp() {
* From MIDlet.
* Signals the MIDlet to enter the Paused state.

public void pauseApp() {
// No implementation required
* From MIDlet.
* Signals the MIDlet to terminate and enter the Destroyed state.

public void destroyApp(boolean unconditional) {
// No implementation required
* Performs exit from midlet.

private void exitMIDlet() {
* From CommandListener.
* Indicates that a command event has occurred on Displayable d.
* @param cmd - a Command object identifying the command.
* @param d - the Displayable on which this event has occurred.

public void commandAction(Command cmd, Displayable d) {
if(cmd == cmdExit) {


After running this midlet the string "Number of cameras" and a count of cameras on the device are displayed. If the device does not have a camera it will show 0.

Supplementary material

Executables and source files can be found in

This page was last modified on 29 July 2013, at 07:54.
33 page views in the last 30 days.