The bottom of this article has an excellent way of doing it
http://blog.affirmix.com/2009/01/28/getting-started-with-adobe-air-and-sqlite-and-avoiding-the-problems/
You may want to consider reading the comments as well because someone mentions error avoidance by encoding it to base64 before committing and decoding when reading.
For posterity's sake:
This would need to be part of the application code
import flash.display.Bitmap;
import flash.display.Loader;
import flash.filesystem.File;
import flash.net.URLLoader;
import flash.utils.ByteArray;
import mx.graphics.codec.PNGEncoder;
private function selectPicture():void
{
// This little section here creates a file object, and then launches the file browser so that you can select your image
var file:File = File.documentsDirectory;
file.addEventListener(Event.SELECT, handleSelectPicture);
file.browseForOpen("Select Picture");
}
private function handleSelectPicture(event:Event):void
{
// Once the image file has been selected, we now have to load it
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoadPicture);
loader.load(new URLRequest(event.target.url));
}
private function handleLoadPicture(event:Event):void
{
// The first thing that we do is create a Loader object (which is a subclass od DisplayObject)
var loader:Loader = Loader(event.target.loader);
// Next, we cast the loader as a Bitmpa object, as the Bitmap object has function to return a BitmapData object based on the image
var image:Bitmap = Bitmap(loader.content);
var encoder:PNGEncoder = new PNGEncoder();
// The PNGEncoder allows you to convert BitmapData object into a ByteArray, ready for storage in an SQLite blob field
var byteArray:ByteArray = encoder.encode(image.bitmapData);
var statement:SQLStatement = SQLConnectionWrapper.instance.setPicture(1, byteArray);
statement.execute(-1, responder);
}
This would need to be part of SQLConnectionWrapper
private function setPicture(recordId:String, byteArray:ByteArray):void
{
if(!(insertRecord is SQLStatement)){
insertRecord = new SQLStatement();
insertRecord.sqlConnection = connection;
insertRecord.text = "INSERT INTO picture_tbl (record_id, data) VALUES (:recordId, :byteArray)";
}
insertRecord.parameters[":recordId"] = recordId;
// The ByteArray should be added as a parameter; this makes the whole process of storing the image in the blob field very easy
insertRecord.parameters[":byteArray"] = byteArray;
return insertRecord;
}
This would need to be part of the application code
import mx.controls.Image;
// This function would be defined in a Responder object that handles a successful query of picture_tbl
private function handleSuccess(result:SQLResult):void
{
var image:Image = new Image();
image.addEventListener(Event.COMPLETE, handleLoadPicture);
image.load(result.data[0].picture);
}
private function handleLoadPicture(event:Event):void
{
var picture:DisplayObject = DisplayObject(event.target.content);
}
As Requested:
private var myCoverArtLoader:URLLoader;
private function set_coverArt(evt:Event) : void {
coverArtImage = new Image();
var ba:ByteArray = new ByteArray();
ba = myCoverArtLoader.data;
coverArtImage.source = ba;
myCoverArtLoader.removeEventListener(Event.COMPLETE, set_coverArt);
var byteArray:ByteArray = new ByteArray;
byteArray = ba;
var sql:String;
sql = "UPDATE random SET coverArtImage=@cover " +
"WHERE id = @id";
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.parameters["@cover"] = byteArray;
stmt.parameters["@id"] = data.id;
stmt.execute();
}
And then to load it back:
private function select_fromDatabase() : void {
var sql:String = "SELECT coverArtImage FROM random WHERE id = '" + data.id + "'";
stmt = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.addEventListener(SQLEvent.RESULT, handleSuccess);
stmt.execute();
}
private function handleSuccess(evt:SQLEvent): void {
var result:SQLResult = evt.currentTarget.getResult();
if (result.data[0].coverArtImage == null) {
get_cover_xml();
} else {
get_cover_db(result);
}
stmt.removeEventListener(SQLEvent.RESULT, handleSuccess);
}
private function get_cover_db(result:SQLResult) : void {
var ba:ByteArray = new ByteArray();
ba = result.data[0].coverArtImage;
coverArtImage = new Image();
coverArtImage.source = ba;
}
I hope that helps!
Best Answer
So the class in play here is SWFLoader (Image extends SWFLoader). If you passed in the ByteArray or a url than it adds the appropriate listener. What you've done is fed it a Bitmap, which is a display object. Here is the important code that is loading a Bitmap into an Image:
ByteArray:
a url:
So you get the gist here. Don't convert it to a Bitmap and you'll get your complete event. You could also add ADDED_TO_STAGE to your Bitmap.