Developing Android Applications with Adobe AIR [46]
import flash.media.CameraRoll;
if (CameraRoll.supportsBrowseForImage == false) {
trace("this device does not support access to the Gallery");
return;
}
If your device does support the Gallery, you can create an instance of the CameraRoll class. Make it a class variable, not a local variable, so that it does not lose scope:
var cameraRoll:CameraRoll = new CameraRoll();
You can add listeners for three events:
A MediaEvent.SELECT when the user selects an image:
import flash.events.MediaEvent;
cameraRoll.addEventListener(MediaEvent.SELECT, onSelect);
An Event.CANCEL event if the user opts out of the Gallery:
import flash.events.Event;
cameraRoll.addEventListener(Event.CANCEL, onCancel);
function onCancel(event:Event):void {
trace("user left the Gallery", event.type);
}
An ErrorEvent.ERROR event if there is an issue in the process:
import flash.events.ErrorEvent;
cameraRoll.addEventListener(ErrorEvent.ERROR, onError);
function onError(event:Event):void {
trace("Gallery error", event.type);
}
Call the browseForImage function to bring the Gallery application to the foreground:
cameraRoll.browseForImage();
Your application moves to the background and the Gallery interface is displayed, as shown in Figure 9-2.
Figure 9-2. The Gallery interface
NOTE
When you are using the native camera, it comes to the foreground, your AIR application moves to the background, and the NativeApplication Event.DEACTIVATE event is called. Make sure that if you registered for that event, you don’t have any logic that could interfere with the running of your application, such as exiting. Likewise, when the native camera application quits and your AIR application comes back to the foreground, Event.ACTIVATE is called.
When you select an image, a MediaEvent object is returned. Use its data property to reference the image and cast it as MediaPromise. Use a Loader object to load the image:
import flash.display.Loader;
import flash.events.IOErrorEvent;
import flash.events.MediaEvent;
import flash.media.MediaPromise;
function onSelect(event:MediaEvent):void {
var promise:MediaPromise = event.data as MediaPromise;
var loader:Loader = new Loader()
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
loader.loadFilePromise(promise);
}
The concept of MediaPromise was first introduced on the desktop in a drag-and-drop scenario where an object doesn’t yet exist in AIR but needs to be referenced. Access its file property if you want to retrieve the image name, its nativePath, or its url.
The url is the qualified domain name to use to load an image. The nativePath refers to the hierarchical directory structure:
promise.file.name;
promise.file.url;
promise.file.nativePath;
Let’s now display the image:
function onImageLoaded(event:Event):void {
addChild(event.currentTarget.content);
}
Only the upper-left portion of the image is visible. This is because the resolution of the camera device is much larger than your AIR application stage.
Let’s modify our code so that we can drag the image around and see all of its content. We will make the image a child of a sprite, which can be dragged around:
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.geom.Rectangle;
var rectangle:Rectangle;
function onImageLoaded(event:Event):void {
var container:Sprite = new Sprite();
var image:DisplayObject = event.currentTarget.content as DisplayObject;
container.addChild(image);
addChild(container);
// set a constraint rectangle to define the draggable area
rectangle = new Rectangle(0, 0,
-(image.width - stage.stageWidth),
-(image.height - stage.stageHeight)
);
container.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
container.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onDown(event:MouseEvent):void {
event.currentTarget.startDrag(false, rectangle);
}
function onUp(event:MouseEvent):void {
event.currentTarget.stopDrag();
}
It may be interesting