Flex/AIR/Actionscript/Mobile File.writeObject/readObject 总是生成 null,没有生成错误 [英] Flex/AIR/Actionscript/Mobile File.writeObject/readObject always generates null w/no errors generated
问题描述
我已经尝试了几天,只是为了在我的项目中获得一个简单的 writeObject/readObject 功能.无论我做什么,结果都是,a) 创建了一个文件,b) 当我尝试读回该文件时,它是 null.
I've been trying for several days just to get a simple writeObject/readObject functionality in my project. No matter what I do, the result is, a) a file is created and b) when I try to read that file back in, it is null.
我的项目面向 iPad 设备上的 iOS,但这个精简版面向 iPad 的 AIR 模拟器.
My project targets iOS on an iPad device, but this stripped down version targets the AIR simulator for an iPad.
我之前已经注册和跟踪了每个可用的事件,但它们从未完成任何事情,因此为了使问题简单起见,我将它们删除了.
I've previously had every single available event registered and traced, but they never accomplished anything so I removed them for the sake of keeping the problem simple.
即使我使用具有单个字符串属性集的通用对象而不是我的值对象,它仍会以 null 形式回读(我假设是写出).
Even if I use a generic object with a single string property set instead of my value object, it still reads back (and I assume, writes out) as null.
这是我的跟踪返回,后面是项目中使用的代码:
Here is my trace return followed by the code used in the project:
[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:
Below is my application 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 类
FileSerializer.as class
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 值对象
PTotmImageVO.as value object
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
推荐答案
已解决.元数据标签是 [RemoteClass... 不是 [remoteClass,如我开始工作的示例代码所示.由于编译器不会检查这些标签,而且制作您自己的标签在技术上也不是错误,因此 - 永远不会 - 任何可用于工作的诊断数据.
Resolved. The Metadata tag is [RemoteClass... not [remoteClass as was shown in the example code I began working from. As the compiler doesn't check these tags, and making up your own tag isn't technically an error, there will -NEVER- be any diagnostic data from which to work.
这篇关于Flex/AIR/Actionscript/Mobile File.writeObject/readObject 总是生成 null,没有生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!