Not yet rated

Problem

I would like to take an image my application loads via HTTP and add it to the CameraRoll on an application that supports both Android and iOS devices.

Solution

Use the Loader class to load the image and then add the loaded BitmapData object to the CameraRoll

Detailed explanation

First, create a Loader object that takes care of loading the image via HTTP. After the loading is finished, grab the BitmapData from the Loader and add it to the CameraRoll.

private function loadImage(urlToImage:String):void
{
  // create the Loader
  var loader:Loader = new Loader();
  // add listener
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  // load the image
  loader.load(new URLRequest(urlToImage));
}

private function completeHandler(event:Event):void
{
  // check if we can add to the CameraRoll
  if (CameraRoll.supportsAddBitmapData)
  {
    var loader:Loader = LoaderInfo(event.target).loader;
    var cameraRoll:CameraRoll = new CameraRoll();
    // add the BitmapData (which lives in the Bitmap loaded by the Loader)
    // to the CameraRoll
    cameraRoll.addBitmapData((loader.content as Bitmap).bitmapData);
  }
}
  

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes