AIRã®SQLiteã§ç»åãæ ¼ç´ããã表示ããæ¹æ³
SQLiteã§ãç»åãæ ¼ç´ãããåãåºãã¦è¡¨ç¤ºã¨ããã¦ã¿ããã
ã£ã¦ãã¨ã§ãã£ã¦ã¿ã¾ããã
åä½ã ã¼ãã¼ã¯ãã¡ã
çµå±ã®ã¨ããç»åãã¼ã¿ãããã¹ãã§ãã¼ã¿ãã¼ã¹ã¸ä¿åãããããªãã£ã½ãã¦Base64ã£ã¦ã¡ã¼ã«ã§ã使ããã¦ãããã®ã使ç¨ãã¾ãã
ãã¼ããã¼ã¿ãBase64ã¸ã¨ã³ã³ã¼ã
var enc:Base64Encoder = new Base64Encoder();
enc.encodeBytes(ByteArray(URLLoader(e.target).data));
Base64ãªããã¹ããã¼ã¿ããã³ã¼ã
var dec:Base64Decoder = new Base64Decoder(); dec.decode(myData[0].image_txt); ldr.loadBytes(ByteArray(dec.flush()));
ï¼å°æ¥çã«PNGdecodeãJPGdecodeãã§ã¦ãããã¨ãããªãã
ï¼ãããBase64å½¢å¼ããµãã¼ããã¦ã
ã½ã¼ã¹ã³ã¼ãã¯ãã¡ã
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" width="817" height="502" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import mx.utils.Base64Decoder; import mx.utils.Base64Encoder; private var conn:SQLConnection; private var stmt:SQLStatement; //åæåé¢æ° private function init():void{ //ãã¼ã¿ãã¼ã¹ã®è¨å® setDB(); //ãã¼ã¿ãã¼ã¹ããæ¢åãã¼ã¿ã®è¡¨ç¤º getData(); } /******************************* DBåæè¨å® ********************************/ //ãã¼ã¿ãã¼ã¹ã³ãã¯ã private function setDB():void{ //ãã¼ã¿ãã¼ã¹ãã¡ã¤ã«ãä½æ var dbfile:File = File.desktopDirectory; dbfile = dbfile.resolvePath("sqliteImage.db"); conn = new SQLConnection(); conn.open(dbfile); //DBãã¼ãã«åæä½æ createDB(); } //ãã¼ã¿ãã¼ã¹ä½æ private function createDB():void{ //ãã¼ã¿ãã¼ã¹ãä½æ stmt = new SQLStatement(); stmt.sqlConnection = conn; stmt.text = " CREATE TABLE IF NOT EXISTS sqliteImage ( " + " id INTEGER PRIMARY KEY , " + " image_txt TEXT , " + " print INTEGER ) ; "; stmt.addEventListener(SQLEvent.RESULT , function():void{}); stmt.execute(); } /******************************* æ¬ãã¼ã¿è¡¨ç¤ºã»è¿½å ã»åé¤ ********************************/ //ãã¼ã¿è¡¨ç¤º private function getData():void{ //ãã¼ã¿ãã¼ã¹ãã表示 stmt = new SQLStatement(); stmt.sqlConnection = conn; stmt.text = " SELECT * FROM sqliteImage ORDER BY id DESC "; //SQLå®äºã¤ãã³ã stmt.addEventListener(SQLEvent.RESULT , function():void{ //çµæã»ããããªãã¸ã§ã¯ãã«å ¥ãã var myData:Object = new Object(); myData = stmt.getResult().data; if( myData != null){ //ãã¼ã¿ããã¼ããã¦Base64ã¸ãã³ã¼ã var ldr:Loader = new Loader(); var dec:Base64Decoder = new Base64Decoder(); dec.decode(myData[0].image_txt); ldr.loadBytes(ByteArray(dec.flush())); //ã¹ãã¼ã¸ã«addChild myImageOut.addChild(ldr); myLog3.text = myData[0].image_txt; dg.dataProvider = myData; } }); try{ stmt.execute(); }catch(e:SQLError){ myLog2.text = "error1"; } } //ãã¼ã¿ã¼ã®è¿½å private function addData():void{ //ãã¤ããªãã¼ã¿ã¨ãã¦ãã¼ã var urlldr:URLLoader = new URLLoader(); urlldr.dataFormat = URLLoaderDataFormat.BINARY; //ãã¼ãå®äºã¤ãã³ã urlldr.addEventListener(Event.COMPLETE, function(e:Event):void{ //ãã¼ããã¼ã¿ãBase64ã¸ã¨ã³ã³ã¼ã var enc:Base64Encoder = new Base64Encoder(); enc.encodeBytes(ByteArray(URLLoader(e.target).data)); //ãã¼ã¿ãã¼ã¹ã¸æ¿å ¥ stmt = new SQLStatement(); stmt.sqlConnection = conn; stmt.text = " INSERT INTO sqliteImage( image_txt )VALUES( '" + enc.flush() + "' )"; myLog.text += stmt.text; stmt.addEventListener(SQLEvent.RESULT , function():void{ getData(); } ); try{ stmt.execute(); }catch(e:SQLError){ myLog2.text = "error2"; } }); //ãã¼ãéå§ urlldr.load(new URLRequest("19.jpg")); } ]]> </mx:Script> <mx:Button x="132" y="51" label="ãã¼ã¿ãã¼ã¹ã«ç»åã追å " click="addData()" height="59"/> <mx:Image x="10" y="51" width="105" height="121" id="myImage" source="19.jpg"/> <mx:Image x="10" y="206" width="105" height="121" id="myImageOut"/> <mx:TextArea x="294" y="51" width="248" height="203" id="myLog"/> <mx:TextArea x="294" y="288" width="248" height="53" id="myLog2"/> <mx:TextArea x="550" y="51" width="255" height="203" id="myLog3"/> <mx:Label x="294" y="25" text="INSERT"/> <mx:Label x="550" y="25" text="SELECT"/> <mx:Label x="294" y="262" text="ERROR"/> <mx:Label x="10" y="25" text="å ¥åç»å"/> <mx:Label x="10" y="180" text="åºåç»å"/> <mx:DataGrid x="550" y="262" width="255" height="202" id="dg" rowHeight="30"> <mx:columns> <mx:DataGridColumn headerText="å 1" dataField="id"/> <mx:DataGridColumn headerText="å 2" dataField="image_txt"/> <mx:DataGridColumn headerText="å 3" dataField="print"/> </mx:columns> </mx:DataGrid> </mx:WindowedApplication>