Skip to main content
Inspiring
April 21, 2015
Question

Save / Load Image on device storage (iOS & Android)

  • April 21, 2015
  • 1 reply
  • 4236 views

Has anyone used application storage on iOS / Android ?

I need to be able to take a photo with the device and save that photo to the application rather than the device camera roll. When my app is opened I need to read these images and load them.

Any help with how to tackle this would be highly appreciated.

Cheers!

This topic has been closed for replies.

1 reply

Inspiring
April 21, 2015

That's an easy one, you just need to use applicationStorageDirectory. Use this code:

//This is the folder you'll be saving the images on

var appStorage:File = File.applicationStorageDirectory.resolvePath("images/");

//This is the image file. We'll be saving it on png format

var imgFile:File = appStorage.resolvePath("yourImage.png")

//This is the FileStream object you'll be using to write the image data to the folder

var fileStream:FileStream = new FileStream;

//To save the image as png format you'll have to encode the image data first

//this is the mx.graphics.codec.PNGEncoder library

var pngEnc:PNGEncoder = new PNGEncoder;

//You can get a ByteArray by passing your image's BitmapData to the encode function

var byteArray:ByteArray = pngEnc.encode(bitmapData);

fileStream.open(imgFile, FileMode.WRITE);

fileStream.writeBytes(byteArray);

fileStream.close();

I think I got everything right. Try it and see the result. To load the image, just use the Loader class, you can find tons of tutorials about that. But if you need help with that too, just say it.

Applauz78Author
Inspiring
April 22, 2015

So I'm able to get the camera to take the photo or choose from library and get the image added to the stage.  Once I get the image added to the stage.. how do I save that image?     and then when my app is opened .. how do I check if that image exists and then load it up?

Ideally ..  I would like to check if  1.png exists .. if it does.. add it to the stage in a predefined space. Hopefully that makes some sense

function addToStage():void {

 

   trace("Image was added to Stage");

  imgLoader = new Loader();

  imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, drawBitmap);

  imgLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandlerIOErrorEventHandler);

  imgLoader.loadFilePromise(imagePromise);

}

function drawBitmap(e:Event):void{

 

  if (image != null && contains(image)){

mcHolder.mcImg.removeChild(image);

}

 

myBitmapData = new BitmapData(600,600);

   image = new Bitmap(myBitmapData);

   image.bitmapData = Bitmap(e.currentTarget.content).bitmapData;

   trace("Original Image Width = " + image.width + " Height = " + image.height);

   trace("Bitmap was drawn");

  mcHolder.mcImg.addChild(image);

  createGestouch();

  

  

  }

Inspiring
April 22, 2015

To save the image, use that code I gave you, but in this line you replace "bitmapData" with "image.bitmapData"

var byteArray:ByteArray = pngEnc.encode(bitmapData);

To check if the image exists use:

var imgFile:File = File.applicationStorageDirectory.resolvePath("1.png");

//It's that simple :D

if(imgFile.exists)

{

    // load image from file

}

else

{

    //load from cameraRoll and than save

}


Hope this helps