Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [47]

By Root 2513 0
to see the details of an image at its full resolution, but this might not result in the best user experience. Also, because camera resolution is so high on most devices, there is a risk of exhausting RAM and running out of memory.

Let’s now store the content in a BitmapData, display it in a Bitmap, and scale the bitmap to fit our stage in AIR. We will use the Nexus One as our benchmark first. Its camera has a resolution of 2,592×1,944. The default template size on AIR for Android is 800×480. To complicate things, the aspect ratio is different. In order to preserve the image fidelity and fill up the screen, you need to resize the aspect ratio to 800×600, but some of the image will be out of bounds.

Instead, let’s resize the image to 640×480. The image will not cover the whole stage, but it will be fully visible. Take this into account when designing your screen.

First, detect the orientation of your image. Resize it accordingly using constant values, and rotate the image if it is in landscape mode:

import flash.display.Bitmap;

import flash.display.BitmapData;

const MAX_HEIGHT:int = 640;

const MAX_WIDTH:int = 480;

function onImageLoaded(event:Event):void {

var bitmapData:BitmapData = Bitmap(event.target.content).bitmapData;

var bitmap:Bitmap = new Bitmap(bitmapData);

// determine the image orientation

var isPortrait:Boolean = (bitmapData.height/bitmapData.width) > 1.0;

if (isPortrait) {

bitmap.width = MAX_WIDTH;

bitmap.height = MAX_HEIGHT;

} else {

bitmap.width = MAX_HEIGHT;

bitmap.height = MAX_WIDTH;

// rotate a landscape image

bitmap.y = MAX_HEIGHT;

bitmap.rotation = -90;

}

addChild(bitmap);

}

The preceding code is customized to the Nexus One, and it will not display well for devices with a different camera resolution or screen size. We need a more universal solution.

The next example shows how to resize the image according to the dynamic dimension of both the image and the stage. This is the preferred approach for developing on multiple screens:

function onImageLoaded(event:Event):void {

var bitmapData:BitmapData = Bitmap(event.target.content).bitmapData;

var bitmap:Bitmap = new Bitmap(bitmapData);

// determine the image orientation

var isPortrait:Boolean = (bitmapData.height/bitmapData.width) > 1.0;

// choose the smallest value between stage width and height

var forRatio:int = Math.min(stage.stageHeight, stage.stageWidth);

// calculate the scaling ratio to apply to the image

var ratio:Number;

if (isPortrait) {

ratio = forRatio/bitmapData.width;

} else {

ratio = forRatio/bitmapData.height;

}

bitmap.width = bitmapData.width * ratio;

bitmap.height = bitmapData.height * ratio;

// rotate a landscape image and move down to fit to the top corner

if (!isPortrait) { bitmap.y = bitmap.width;

bitmap.rotation = -90;

}

addChild(bitmap);

}

Beware that the browseForImage method is only meant to load images from the Gallery. It is not for loading images from the filesystem even if you navigate to the Gallery. Some devices bring up a dialog to choose between Gallery and Files. If you try to load an image via Files, the application throws an error. Until this bug is fixed, set a listener to catch the error and inform the user:

cameraRoll.browseForImage();

cameraRoll.addEventListener(ErrorEvent.ERROR, onError);

function onError(event:Event):void {

if (event.errorID == 2124) {

trace("you can only load images from the Gallery");

}

}

If you want to get a list of all the images in your Gallery, you can use the filesystem as follows:

var gallery:File = File.userDirectory.resolvePath("DCIM/Camera");

var myPhotos:Array = gallery.getDirectoryListing();

var bounds:int = myPhotos.length;

for (var i:uint = 0; i < bounds; i++) {

trace(myPhotos[i].name, myPhotos[i].nativePath);

}

Adding an Image


You can add an image to the Gallery from within AIR. To write data to the SD card, you must set permission for it:

Check the supportsAddBitmapData property to verify that your device supports

Return Main Page Previous Page Next Page

®Online Book Reader