0

我已经尝试了几天,只是为了在我的项目中获得一个简单的 writeObject/readObject 功能。无论我做什么,结果都是,a)创建了一个文件,并且 b)当我尝试重新读取该文件时,它是null

我的项目针对 iPad 设备上的 iOS,但这个精简版针对 iPad 的 AIR 模拟器。

我之前已经注册和跟踪了每个可用的事件,但它们从未完成任何事情,所以为了保持问题简单,我删除了它们。

即使我使用带有单个字符串属性集的通用对象而不是我的值对象,它仍然会读回(并且我假设,写出)为null

这是我的跟踪返回,后跟项目中使用的代码:

[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
4

1 回答 1

0

解决。Metadata 标记是 [RemoteClass... 而不是 [remoteClass,如我开始工作的示例代码中所示。由于编译器不检查这些标签,并且制作您自己的标签在技术上并不是错误,因此 - 永远 - 不会有任何诊断数据可以工作。

于 2013-01-22T19:57:00.963 回答