In below example for save the text files and image files in Adobe Air. Adobe Air was easily interact with the system local files. In this example i saved the text file as well as image files and it has the format as .jpg. When need to save the text file first it check whether the file are exit or not. If the file cannot be found means then the text file content saved as a new file. This below example save the files using the File Stream class. Try the below example was very nice and intresting one.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.graphics.codec.PNGEncoder;
import flash.filesystem.*;
private var _stream:FileStream;
private function saveFile():void
{
var file:File = File.desktopDirectory.resolvePath("Test.txt");
_stream = new FileStream();
_stream.open(file,FileMode.WRITE);
_stream.writeUTFBytes(txt_area.text);
_stream.close();
}
private function saveImage():void
{
var file:File = File.desktopDirectory.resolvePath("test.jpg");
_stream = new FileStream();
_stream.open(file,FileMode.WRITE);
var _enc:PNGEncoder = new PNGEncoder();
var _bitmap:BitmapData = new BitmapData(img.width,img.height,true);
_bitmap.draw(img,new Matrix());
var bytes:ByteArray = _enc.encode(_bitmap);
_stream.writeBytes(bytes);
_stream.close();
}
]]>
</mx:Script>
<mx:VBox paddingTop="15" paddingLeft="15" paddingRight="15" paddingBottom="15">
<mx:HBox>
<mx:Label text="About Yourself:"/>
<mx:TextArea id="txt_area" width="250" height="125"/>
</mx:HBox>
<mx:HBox width="100%" horizontalAlign="center">
<mx:Image id="img" source="Test.jpg" width="200" height="150" horizontalAlign="center"/>
</mx:HBox>
<mx:HBox>
<mx:Button label="Save" click="saveFile()"/>
<mx:Button label="Save Image" click="saveImage()"/>
</mx:HBox>
</mx:VBox>
</mx:WindowedApplication>
No comments:
Post a Comment