Write Local Image with AIR

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.

corelib-googlesite


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:

interface


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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值