It is really disappointing, and frustrating when seeing as3corelib's official site. It seems that this project was shutdown and not supported any more, since two years ago.
We can see that all of the files on the Google Code page are deprecated.
Anyway, we can load the source code from here: https://github.com/mikechambers/as3corelib
Code Worked Out
After reading the references at the end of this blog. I combine the codes and work out the solution, as:
this is document class definition:
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.filesystem.*;
import com.adobe.images.PNGEncoder;
public class main extends MovieClip
{
private static const IMAGE_FOLDER:String = "SaveImages/";
private var _bmd:BitmapData;
private var _draw:Drawings;
private var _imgByteArray:ByteArray;
public function main()
{
this.addEventListener(Event.ADDED_TO_STAGE, this.addedToStagedHandler);
}
private function getNowTimestamp():String
{
var $d:Date = new Date();
var $tstamp:String = $d.getFullYear().toString() +
$d.getMonth() + $d.getDate() + $d.getHours() + $d.getMinutes() + $d.getSeconds() + $d.getMilliseconds();
return $tstamp;
}
// HANDLERS:
private function addedToStagedHandler($e:Event):void
{
this._draw = new Drawings();
this._draw.x = 100;
this._draw.y = 10;
this.addChild(this._draw);
saveBtn.addEventListener(MouseEvent.CLICK, this.saveButtonClickedHandler);
this._bmd = new BitmapData(this._draw.width, this._draw.height, true, 0x00FFFFFF);
this._bmd.draw(this._draw);
}
private function saveButtonClickedHandler($e:MouseEvent):void
{
this._imgByteArray = PNGEncoder.encode(this._bmd);
var $fs:FileStream = new FileStream();
var $fileName:String = "myDrawings" + this.getNowTimestamp() + ".png";
var $path:String = "app:/" + IMAGE_FOLDER + $fileName;
//var $fl:File = File.applicationDirectory.resolvePath(IMAGE_FOLDER + $fileName); // get Error:fileWriteResource
//var $fl:File = File.applicationStorageDirectory.resolvePath(IMAGE_FOLDER + $fileName); // get Error:fileWriteResource
//var $fl:File = new File($path); // get Error:fileWriteResource
var $fl:File = new File("D:/Work-Complex-For-IDS/Lab/saving-inages/" + $fileName); // works fine
try{
$fs.open($fl,FileMode.WRITE);
$fs.writeBytes(this._imgByteArray);
$fs.close();
}
catch(e:Error)
{
trace(e.message);
}
}
}
}
this application's interface is:
One point to highlight:
It is about the directory to write/read files. I have encountered the problem before, and you can referFile doc for the paths it provide. I you write file to File.documentsDirectory, then it works Okay, but you will find the file at: C:\Documents and Settings\[your account name]\My Documents\SaveImages
Usually, you might want to save the files under the same directory as your app installation, then you may use File.applicationDirectory or File.applicationStorageDirectory, but you will get error: fileWriteResource, and one solution is to create file instance by using File constructor, and pass a path as string to it. Please refer to http://forums.adobe.com/thread/209533
Refs:
http://cookbooks.adobe.com/post_Save_a_local_image_file__JPEG_or_PNG__from_an_imag-8406.html
http://blog.stroep.nl/2008/09/saving-images-with-air-create-a-wallpaper-using-as3-only/
http://www.thetechlabs.com/tech-tutorials/flash/saving-user-generated-imagery-from-an-air-application-using-the-jpg-and-png-encoder-classes-and-flash-cs4/
http://forums.adobe.com/thread/209533
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html