The Camera Application and the CameraUI Class

Must Read

Just post!

You can access the native camera within AIR. Your application needs to have the permission for it. In Flash Professional, select File→AIR Android settings→Permissions→Camera. In Flash Builder, select CAMERA under Mobile Settings→

The class is an addition to the ActionScript language to support the device’s native camera application. It is a subclass of the EventDispatcher class. It is only supported on AIR for mobile applications.

This object allows you to launch the native camera application to take an image or shoot a video while your AIR application moves to the background. The image is stored in the Gallery along with the other images.

To use this class, first you must verify that your device supports access to the camera by checking the CameraUI.isSupported property:

if (CameraUI.isSupported == false) {
trace(“You cannot use the native camera.”);

If camera access is supported, create an instance of the CameraUI class and call its launch function. This function expects one parameter of type MediaType to specify picture or video mode. Choosing a level of compression is not an option at the time of this writing:

var cameraUI:CameraUI = new CameraUI();

The camera application is now active and in the foreground. The AIR application moves to the background.

To receive camera events, set listeners before launching the camera. A Media Event.COMPLETE is dispatched after a picture is taken, an Event.CANCEL if no media is selected, and an ErrorEvent if there is an error in the process:

cameraUI.addEventListener(MediaEvent.COMPLETE, onComplete);
cameraUI.addEventListener(Event.CANCEL, onCancel);
cameraUI.addEventListener(ErrorEvent.ERROR, onError);

Once the event is received, the camera application automatically closes and the AIR application moves back to the foreground.

function onComplete(event:MediaEvent):void {
var promise:MediaPromise = as MediaPromise;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
function onImageLoaded(event:Event):void {
var bitmapData:BitmapData = Bitmap(;
var bitmap:Bitmap = new Bitmap(bitmapData);
var isPortrait:Boolean = (bitmapData.height/bitmapData.width) > 1.0;
var forRatio:int = Math.min(stage.stageHeight, stage.stageWidth);
var ratio:Number;
if (isPortrait) {
ratio = forRatio/bitmapData.width;
} else {
ratio = forRatio/bitmapData.height;
bitmap.width = bitmapData.width * ratio;
bitmap.height = bitmapData.height * ratio;
if (!isPortrait) {
bitmap.y = stage.stageHeight;
bitmap.rotation = -90;

Uploading to a Remote Server

Images can be uploaded to a remote server if you have access to one. You need the Internet permission to add this functionality:

<uses-permission android:name=”android.permission.INTERNET” />

This process is identical to what you would do in a desktop application:

import flash.filesystem.File;
var request:URLRequest = new URLRequest(“server url”);
request.method = URLRequestMethod.POST;
var uploadFile:File = new File(promise.file.url);


- Advertisement -

Latest News

GAC Group’s Global Expansion Strategy: Key Details Revealed

On August 25th, GAC Group released its 2023 semi-annual report, showing a total operating revenue of 233.532 billion yuan...
- Advertisement -

More Articles Like This

- Advertisement -