Skip to content

Instantly share code, notes, and snippets.

@itoz itoz/file0.txt
Created Jun 20, 2012

Embed
What would you like to do?
[AIR] ローカルに画像保存 ref: http://qiita.com/items/7ba6dcd5fe6ecbe6e196
/**
* copyright (c) 2012 www.itoz.jp
* @author itoz
* @since 2012/06/20 19:55:22
*/
package jp.itoz.air.util
{
import flash.events.ErrorEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.ByteArray;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
/**
* [AIR] ローカルに画像保存
*/
public class LocalImageSaver extends EventDispatcher
{
public function LocalImageSaver(target : IEventDispatcher = null)
{
super(target);
}
/**
* 保存
* @param writeFile
* @param imageBytes
*/
public function save(writeFile : File, imageBytes : ByteArray) : void
{
var stream : FileStream = new FileStream();
try {
stream.open(writeFile, FileMode.WRITE);
stream.writeBytes(imageBytes, 0, imageBytes.length);
}
catch (e : Error) {
dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, writeFile + " 保存できませんでした"));
}
finally {
stream.close();
}
}
}
}
//sample
_saveDir = File.userDirectory;
var pngBytes : ByteArray = PNGEncoder.encode(myBitmapData);
_saver = new LocalImageSaver();
_saver.addEventListener(ErrorEvent.ERROR, onSaveError);
_saver.save(_saveDir.resolvePath("hoge"+"/"+_name), pngBytes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.