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
decibell
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook
Accepted Solution

Saving Syntax Incorrect?

[ Edited ]

Hey Everyone,

 

When trying to save an image through use of a ByteArray I'm getting a "Error #2038: File I/O Error." error...

 

Is my syntax incorrect?

 

 

var loader:Loader = (e.target as LoaderInfo).loader;
			
var bitmapData:BitmapData = new BitmapData(loader.width, loader.height);
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var imgData:ByteArray = jpgEncoder.encode(bitmapData);
			
var imgFileToSave:File = File.applicationStorageDirectory;	
imgFileToSave.addEventListener(Event.COMPLETE, imageFileDidSave);
			imgFileToSave.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void
{
     trace(e.toString());
});
imgFileToSave.save(imgData, "Image.jpg");

 *scratches head*

 

----------------------------
Let's turn things up...
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving Syntax Incorrect?

[ Edited ]

hey,

 

i havent tested it out but at first glance this line:

 

 

imgData.draw(loader);

 

seems out of place and premature? imgData is defined later. dont know if that was a typo though.

 

Edit: at a second glance it seems that line should be:

 

bitmapData.draw(loader);

 

good luck!

 

Edit #2: just saw your reply. i'll keep testing!

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
Developer
decibell
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Saving Syntax Incorrect?

ops that was a typo on the forums, not my actual code 

----------------------------
Let's turn things up...
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Saving Syntax Incorrect?

This is from a screen capture code of mine.  Adapt as needed.

 

private function SaveImage( view : DisplayObject ) : void
{  
    var jpg_encoder : JPEGEncoder = new JPEGEncoder( 100 );  
      
    var img: ImageSnapshot = ImageSnapshot.captureImage( view, 0, jpg_encoder );

      
    var download_file_ref : FileReference = new FileReference();
    download_file_ref.save( img.data, "image.jpg" );
}

 If this does not help or you still get an error, try it in AIR.  If OK in AIR, then it is a simulator issue.

Developer
decibell
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Saving Syntax Incorrect?

yea it's fine in AIR which is why I'm confused... must be a simulator issue

----------------------------
Let's turn things up...
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Saving Syntax Incorrect?

[ Edited ]

Try not saving in the application storage directory.  Use my little example to save the file.  It may be a a tighter sandbox on the simulator.  Really should not be able to browse that directory anyway.

 

var download_file_ref : FileReference = new FileReference();
    download_file_ref.save( img.data, "image.jpg" );

Developer
decibell
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Saving Syntax Incorrect?

yup, exact same error :\

----------------------------
Let's turn things up...
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Saving Syntax Incorrect?

hey decibell,

 

what does your loader look like? the one you attached the listener function to.

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
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Saving Syntax Incorrect?

You may want to report that to the BB issues database.

 

http://us.blackberry.com/developers/resources/issuetracker/

 

 

Developer
decibell
Posts: 73
Registered: ‎11-16-2010
My Device: Playbook

Re: Saving Syntax Incorrect?

[ Edited ]

JRab, it's not too complex, like I said it works fine on AIR

jtegen : I think I'm going to

 

private function fetchImage():void
{			
        var urlLoader:URLLoader = new URLLoader();
	urlLoader.addEventListener(Event.COMPLETE, htmlDidLoad);
			
	urlLoader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void
	{
	var alertDialog:AlertDialog = new AlertDialog();
				alertDialog.dialogSize = DialogSize.SIZE_SMALL;
				alertDialog.message = "Sorry, a valid internet or data connection could not be made! Please check your connection and try again later.";
			});
			
			var request:URLRequest = new URLRequest("http://www.somewebsite.com/");
			urlLoader.load(request);
		}
		
		private function htmlDidLoad(e:Event):void
		{
			var htmlData:String = String(e.target.data);
			var htmlRegData:Array = htmlData.match(/someregex/);
			var imageURLData:String = htmlRegData[0];

			var htmlPrefix:String = "http://www.somewebsite.com";
			
			var imgFileToSave:File = File.applicationStorageDirectory;	
			
			
			if (!imgFileToSave.resolvePath(imageURL.substring(28, imageURL.length)).exists)
			{
				imageURL = htmlPrefix.concat(imageURLData);
				
				var loader:Loader = new Loader;
				var imgReq:URLRequest = new URLRequest(imageURL);
				loader.load(imgReq);
				
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageDidLoad);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function():void
				{
					var alertDialog:AlertDialog = new AlertDialog();
					alertDialog.dialogSize = DialogSize.SIZE_SMALL;
					alertDialog.message = "Sorry, the internet connection has been lost! Please check your connection and try again later.";
				});
			}
			else
			{
				var alertDialog:AlertDialog = new AlertDialog();
				alertDialog.dialogSize = DialogSize.SIZE_SMALL;
				alertDialog.message = "somestuff";
			}
		}

 edited some stuff out to not give away the app :smileywink:

 

https://www.blackberry.com/jira/browse/TABLET-74 <-- please vote on the issue!!

----------------------------
Let's turn things up...