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
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving Syntax Incorrect?

hey decibell,

 

thanks for the updated code it helped! and no worries i totally understand about the whole not giving it away part :smileyhappy:

 

as for the problem i've narrowed it down. first thing the reason why your code isnt working is because it uses the OS's save dialog which is related to the .browse(), .browseDirectory(), etc. its been noted (and i've tested) that the whole file browsing structure in playbook simulator is broken. so that explains why you get the weird IOError. its probably not a finished part of the AIR platform on the simulator.

 

as for the workaround, you need to use other methods to save your file other than using the .save() method of the File (or FileReference) class until the whole browsing issue is fixed in the simulator. below is a working code of a version of a workaround. it uses the filestream class that than takes your data and writes to a file. if you want to have the user edit the file name, you will have to create a way to do so otherwise the names have to be generated by your code:

 

 

var loader:Loader = (e.target as LoaderInfo).loader;

var bitmapData:BitmapData = new BitmapData(loader.width, loader.height);

bitmapData.draw(loader.content);

var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var imgData:ByteArray = jpgEncoder.encode(bitmapData);

var imgFileToSave:File = new File(File.applicationStorageDirectory.resolvePath("Image.png").nativePath);

var fs:FileStream = new FileStream();

fs.open(imgFileToSave, FileMode.WRITE);
fs.writeBytes(imgData);
fs.close();

 

 

also of note you need the bitmapData.draw() portion in order to properly write an image using the bitmapData object.

 

Below is the code i used to check its effectiveness:

 

 

package
{
	import com.adobe.images.JPGEncoder;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import qnx.ui.display.Image;
	
	[SWF(height="600", width="1024", frameRate="30", backgroundColor="#BBBBBB")]
	public class ImageTest extends Sprite
	{	
		private var myBitmap:Bitmap;
		private var myLoader:Loader;
		private var myRequest:URLRequest;
		
		private var myImage:Image;
		
		public function ImageTest()
		{
			super();
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			myLoader = new Loader();
			myRequest = new URLRequest("http://www.google.com/images/logos/ps_logo2.png");
			myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			
			
			var myFile:File = File.applicationStorageDirectory.resolvePath("myImage.png");
			
			myImage = new Image();
			
			addChild(myImage);
			
			if (myFile.exists)
			{
				myFile.deleteFile();
			}
			else
			{
				myLoader.load(myRequest);
			}
			
		}
		
		private function onComplete(e:Event):void
		{
			
			var loader:Loader = (e.target as LoaderInfo).loader;
			
			var bitmapData:BitmapData = new BitmapData(loader.width, loader.height);
			
			bitmapData.draw(loader.content);
			
			var jpgEncoder:JPGEncoder = new JPGEncoder(80);
			var imgData:ByteArray = jpgEncoder.encode(bitmapData);
			
			var imgFileToSave:File = new File(File.applicationStorageDirectory.resolvePath("myImage.png").nativePath);	
			
			var fs:FileStream = new FileStream();
			
			fs.open(imgFileToSave, FileMode.WRITE);
			fs.writeBytes(imgData);
			fs.close();
			
			myImage.setImage(imgFileToSave.url);
		}
	}
}

 

hope that clears up a few things. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Developer
decibell
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Saving Syntax Incorrect?

Thank you once again JRab!

 

That was actually what I was trying to accomplish, I didn't know that the FileStream worked like that with byteArrays and such, so I resorted to File/FileReference. 

 

Cheers!

----------------------------
Let's turn things up...
Please use plain text.