Flex/AIR/Actionscript/Mobile File.writeObject/readObject always generates null w/no errors generated

China☆狼群 提交于 2019-12-13 04:27:12

问题


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.

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.

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

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

回答1:


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.



来源:https://stackoverflow.com/questions/14366911/flex-air-actionscript-mobile-file-writeobject-readobject-always-generates-null-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!