02-09-2011 06:14 PM - edited 02-09-2011 06:18 PM
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*
Solved! Go to Solution.
02-09-2011 06:16 PM - edited 02-09-2011 06:18 PM
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!
02-09-2011 06:17 PM
ops that was a typo on the forums, not my actual code
02-09-2011 06:23 PM
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.
02-09-2011 06:25 PM
yea it's fine in AIR which is why I'm confused... must be a simulator issue
02-09-2011 06:32 PM - edited 02-09-2011 06:33 PM
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" );
02-09-2011 06:36 PM
yup, exact same error :\
02-09-2011 06:37 PM
hey decibell,
what does your loader look like? the one you attached the listener function to.
02-09-2011 06:37 PM
You may want to report that to the BB issues database.
http://us.blackberry.com/developers/resources/issu
02-09-2011 06:42 PM - edited 02-09-2011 06:55 PM
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.CO MPLETE, imageDidLoad);
loader.contentLoaderInfo.addEventListener(IOErrorE vent.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 ![]()
https://www.blackberry.com/jira/browse/TABLET-74 <-- please vote on the issue!!