03-05-2012 06:52 AM
I want to save the image to application local folder, since my app get slowed while using database to save the images. Can anyone help me with the code.
03-05-2012 07:39 AM
03-20-2012 04:26 AM
Im still stuck. I used FileRefernce class to save the image. But im not able to list it while using browse(). So anyone give me the right code for saving the image and retrieve it back..................
03-20-2012 05:24 AM - edited 03-20-2012 05:24 AM
you shouldn't use filereference when you try something huge, use file and filestream as jtegen said. btw, using file also isn't very fast when you store a large amount of images
heree's a snippet that may be useful, storing bytearrays. you can convert images to bytearrays as well
//storing
function writeFile(file:File, ba:ByteArray):void
{
if(file.exists == false)
{
//create a default file
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.close();
}
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.UPDATE); //tried using async, but that does not necessarily store the images (and also takes a lot time)
fileStream.writeBytes(ba,0,ba.length);
fileStream.close();
}
//reading
var fs:FileStream;
var ba:ByteArray;
var file:File = //get your file
if(file.exists)
{
fs = new FileStream();
ba = new ByteArray;
fs.open(file, FileMode.READ);
fs.readBytes(ba, 0, fs.bytesAvailable);
fs.close();
return ba;
}
03-20-2012 07:03 AM
So how to convert it into image from bytearray. Im used bitmapData but that requires some additional parameter like width and height.
03-20-2012 07:15 AM
a short google search returned this
//bitmapdata to bytearray
var ww:int = bitmapData.width;
var hh:int = bitmapData.height;
var ba:ByteArray = bitmapData.getPixels(new Rectangle(0,0,ww,hh));
//other way around
var bm:BitmapData = new BitmapData(ww, hh);
bm.setPixels(new Rectangle(0,0,ww,hh), ba);
var _img:Image = new Image();
_img.setImage(bm);
you need to store width and height though, and that's where you can use your database, because just storing numbers is faster than images
03-21-2012 01:47 AM
That code works fine for me. Is it possible to create a jpg image from the byte array. And bitmap is working fine for me...........
03-23-2012 06:04 AM
I checked the image size by putting it to document storage area. And i found the image sizes are larger comparitive to its original size.(eg. a 3.6kb image to 56kb and 20kb image around 500kb).
03-23-2012 08:32 AM
yes, that's because the bitmapdata of the image is bein read. so you can imagine that any compressed image like jpeg etc is being put pixel by pixel into a bitmap, making it uncompressed, which is why the image is significantly larger. :/
it also won't help if you tried to zip it, tried that already, didn't work
but maybe you could try to encode the bitmapData as JPEG or something, storing that as JPEGEncoded bytearray and decoding it when read again (google for AS3 jpeg encoder/decoder ?). would be awesome if you could share that
03-23-2012 10:25 AM
Yeah. I will try that and post here the code.