我一直在尝试了好几天只是为了让我的项目一个简单的writeObject /的readObject功能。 无论我做什么,结果是,a)中创建的文件和b)当我尝试读取文件回,它是空的。
我的项目目标的iPad设备上的iOS,但这个精简版的目标为iPad上的AIR模拟器。
我以前有每一个可用的事件注册和跟踪,但他们从来没有,所以我把他们赶走保持问题简单起见,一事无成。
即使我用一个字符串属性设置,而不是我的价值对象的通用对象,它仍然读回(我假设,写出)为空 。
这里是我的跟踪回报,然后在项目中使用的代码:
[SWF] PictureToolsOnTheMoveMakeItDev.swf - 2,644,533 bytes after decompression
PictureToolsOnTheMoveMakeItDev FUNCTION creationCompleteHandler
file.resolvePath(filename).nativePath: C:\Users\cepelc\AppData\Roaming\org.PictureTools.Apps.PictureToolsOnTheMoveMakeItDev.debug\Local Store\User00100\Photos\00100-1358359285139.PTotmImageVO
PictureToolsOnTheMoveMakeItDev FUNCTION saveImageToLibrary
FileSerializer FUNCTION writeObjectToFile()
FileStream.open(write) TRY
FileStream.open(write) FINALLY
FileStream.writeObject(ptotmImageVO) TRY
FileStream.writeObject(ptotmImageVO) FINALLY
FileStream.close()
PictureToolsOnTheMoveMakeItDev FUNCTION readImageFromLibrary
FileSerializer FUNCTION readObjectFromFile(C:\Users\cepelc\AppData\Roaming\org.PictureTools.Apps.PictureToolsOnTheMoveMakeItDev.debug\Local Store\User00100\Photos\00100-1358359285139.PTotmImageVO)
file.exists: true
FileStream.open(read) TRY
FileStream.open(read) FINALLY
FileStream.readObject() TRY
FileStream.readObject() FINALLY
FileStream.close()
FileSerializer FUNCTION readObjectFromFile -- ptotmImageVO -- null
ptotmImageVO: null
[Unload SWF] PictureToolsOnTheMoveMakeItDev.swf
下面是我的应用程序MXML:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="240"
xmlns:c="components.*"
creationComplete="creationCompleteHandler()">
<s:layout>
<s:VerticalLayout verticalAlign="middle" horizontalAlign="center" />
</s:layout>
<fx:Script>
<![CDATA[
import classes.FileSerializer;
import vo.PTotmImageVO;
private var ptotmImageVO:PTotmImageVO;
private var fileSerializer:FileSerializer = new FileSerializer();
private var file:File = File.applicationStorageDirectory;
private var filename:String;
protected function creationCompleteHandler():void
{
trace("PictureToolsOnTheMoveMakeItDev FUNCTION creationCompleteHandler");
ptotmImageVO = new PTotmImageVO();
ptotmImageVO.userid = "00100";
ptotmImageVO.description = "TestPuppyBunnyThingy";
ptotmImageVO.timestamp = new Date().getTime();
ptotmImageVO.type = "PictureTools - On The Move - Photo Entity";
filename = ptotmImageVO.userid+"-"+ptotmImageVO.timestamp+".PTotmImageVO";
file = file.resolvePath("User00100");
if(file.exists && !file.isDirectory)
{
file.deleteFile();
}
file.createDirectory();
file = file.resolvePath("Photos");
if(file.exists && !file.isDirectory)
{
file.deleteFile();
}
file.createDirectory();
trace(" file.resolvePath(filename).nativePath: "+file.resolvePath(filename).nativePath);
saveImageToLibrary();
} // end FUNCTION creationCompleteHandler
protected function saveImageToLibrary():void
{
trace("PictureToolsOnTheMoveMakeItDev FUNCTION saveImageToLibrary");
fileSerializer.writeObjectToFile(ptotmImageVO, file.resolvePath(filename).nativePath);
readImageFromLibrary();
} // end FUNCTION saveImageToLibrary
protected function readImageFromLibrary():void
{
trace("PictureToolsOnTheMoveMakeItDev FUNCTION readImageFromLibrary");
ptotmImageVO = fileSerializer.readObjectFromFile(file.resolvePath(filename).nativePath) as PTotmImageVO;
trace(" ptotmImageVO: "+ptotmImageVO);
} // End FUNCTION readImageFromLibrary
]]>
</fx:Script>
</s:Application>
FileSerializer.as类
package classes
{
import flash.errors.IOError;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import vo.PTotmImageVO;
public class FileSerializer
{
private var fileStream:FileStream = new FileStream();
private var file:File;
public function FileSerializer()
{
} // End CONSTRUCTOR FileSerializer
public function writeObjectToFile(ptotmImageVO:PTotmImageVO, fname:String):void
{
trace("FileSerializer FUNCTION writeObjectToFile()");
file = new File(fname);
try
{
trace(" FileStream.open(write) TRY");
fileStream.open(file, FileMode.WRITE);
}
catch (e:SecurityError)
{
trace(" FileStream.open(write) CATCH SecurityError "+e);
}
finally
{
trace(" FileStream.open(write) FINALLY");
}
try
{
trace(" FileStream.writeObject(ptotmImageVO) TRY");
fileStream.writeObject(ptotmImageVO);
}
catch (e:IOError)
{
trace(" FileStream.writeObject(ptotmImageVO) CATCH IOError "+e);
}
finally
{
trace(" FileStream.writeObject(ptotmImageVO) FINALLY");
}
fileStream.close();
trace(" FileStream.close()");
} // End FUNCTION writeObjectToFile
public function readObjectFromFile(fname:String):PTotmImageVO
{
trace("FileSerializer FUNCTION readObjectFromFile("+fname+")");
var ptotmImageVO:PTotmImageVO;
file = file.resolvePath(fname);
trace(" file.exists: "+file.exists);
if(file.exists)
{
try
{
fileStream.open(file, FileMode.READ);
trace(" FileStream.open(read) TRY");
}
catch (e:SecurityError)
{
trace(" FileStream.open(read) CATCH SecurityError "+e);
}
finally
{
trace(" FileStream.open(read) FINALLY");
}
try
{
trace(" FileStream.readObject() TRY");
ptotmImageVO = fileStream.readObject() as PTotmImageVO;
}
catch (e:IOError)
{
trace(" FileStream.readObject() CATCH IOError "+e);
}
finally
{
trace(" FileStream.readObject() FINALLY");
}
fileStream.close();
trace(" FileStream.close()");
trace(" FileSerializer FUNCTION readObjectFromFile -- ptotmImageVO -- "+ptotmImageVO);
return ptotmImageVO;
}
else
{
return null;
}
} // End FUNCTION readObjectFromFile
} // End CLASS FileSerializer
} // End PACKAGE classes
PTotmImageVO.as值对象
package vo
{
import flash.display.BitmapData;
[remoteClass(alias="PTotmImageVO")]
public class PTotmImageVO
{
public var userid:String;
public var thumbnail:BitmapData;
public var image:BitmapData;
public var timestamp:Number;
public var description:String;
public var type:String;
public function PTotmImageVO()
{
} // End Constructor PTotmImageVO
} // End Class PTotmImageVO
} // End Package vo