Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Code for saving an image to application local folder

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.

Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Code for saving an image to application local folder

I believe you mean the app-storage folder. Just use the File class to manage the file, and FileStream to manage the i/o.
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: Code for saving an image to application local folder

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..................

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Code for saving an image to application local folder

[ Edited ]

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;
						}

 

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: Code for saving an image to application local folder

So how to convert it into image from bytearray. Im used bitmapData but that requires some additional parameter like width and height. 

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Code for saving an image to application local folder

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

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: Code for saving an image to application local folder

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...........

Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: Code for saving an image to application local folder

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).

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Code for saving an image to application local folder

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

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: Code for saving an image to application local folder

Yeah. I will try that and post here the  code.