How to add an image to android gallery (CameraRoll) using adobe air

A very simple and handy task at hand is to save your own visual content into the android gallery. For this Adobe AIR provides the CameraRoll class which takes care of all the protocol negotiations and compression of the image data provided by you. You simply need to create a BitmapData from your visual element and pass it to the CameraRoll. This is important to note that saving an image to the device in this way is much much faster than  encoding it to JPEG o PNG yourself.

Code Explanation

This code demonstrates how to save image data to the Android CameraRoll (Gallery). On the stage we have a image, a text box and a Button. When you press the Capture button, a snapshot of the entire stage is taken using the BitmapData class. Then we create a CameraRoll object and provide it with this bitmapdata. Internally android system applies its standard JPEG compression on this Bitmapdata and saves it as a standard jpeg image to the gallery. you can then navigate to the gallery on the device and view your image.

If you need to add a photo from any other source, you need to load the image into your application and then convert it into a BitmapData object. You can then provide this BitmapData to the CameraRoll instance to save it to the gallery. You can also draw up something using the drawing api in actionscript, convert it to a BitmapData and add it to gallery via CameraRoll.

The Output

save to cameraroll
Save To CameraRoll

 

The Code

import flash.media.CameraRoll;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.display.BitmapData;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.desktop.NativeApplication;
import flash.utils.getTimer;

var cameraRoll:CameraRoll;
var bmp:BitmapData;
var btnCapture:SimpleButton = this.btnCapture;
btnCapture.addEventListener(MouseEvent.CLICK,saveToCameraRoll);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);

function reset(cameraRoll:CameraRoll):void
{
cameraRoll.removeEventListener(Event.COMPLETE, onAddComplete);
cameraRoll.removeEventListener(ErrorEvent.ERROR, onAddError);

bmp.dispose();
bmp = null;
cameraRoll = null;
}

function onAddComplete(e:Event):void
{
var cameraRoll:CameraRoll = e.target as CameraRoll;
reset(cameraRoll);

txtInfo.text = "Saved To CameraRoll";
}

function onAddError(e:Event):void
{
var cameraRoll:CameraRoll = e.target as CameraRoll;
reset(cameraRoll);

txtInfo.text = "Save Failed";
}

function saveToCameraRoll(e:MouseEvent):void
{
cameraRoll = new CameraRoll();
// pritn a timestamp on screen to distinguish saved images
txtInfo.text = flash.utils.getTimer().toString();

if (CameraRoll.supportsAddBitmapData)
{

bmp = new BitmapData(stage.stageWidth, stage.stageHeight);
bmp.draw(stage);

cameraRoll.addEventListener(Event.COMPLETE, onAddComplete);
cameraRoll.addEventListener(ErrorEvent.ERROR, onAddError);
cameraRoll.addBitmapData(bmp);
}
else
{
txtInfo.text = "Feature Not Supported !";
}
}

function onKey(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
e.preventDefault();
NativeApplication.nativeApplication.exit();
}
}

 

Download

 

Download Source

Download APK

3 Comments - Leave a comment
  1. Paul Steven says:

    Any idea why it would fail to save on a Nexus 7? I have installed your APK and I am getting the error “Save Failed”.

    I have this functionality working for an IOS app with more or less the same code and it works fine.

    Any chance there is a setting on the Nexus 7 prohibiting saving?

  2. Paul Steven says:

    Just to add to my last comment, the APK works on my HTC Desire Phone with Android 2.x but the Nexus with Android 4.12 fails to save the image to the camera roll

  3. Paul it fail beacuse maybe you didnt set the permission to save in the memory

Leave a Reply

Your email address will not be published. Required fields are marked *

*