的Flex / AIR /动作脚本/移动File.writeObject /的readObject总是产生空瓦特/生成错误 [英] Flex/AIR/Actionscript/Mobile File.writeObject/readObject always generates null w/no errors generated

查看:130
本文介绍了的Flex / AIR /动作脚本/移动File.writeObject /的readObject总是产生空瓦特/生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试了好几天只是为了获得我的项目一个简单的writeObject /的readObject功能。无论我做什么,结果是,一个)创建一个文件和b),当我尝试读取该文件回来,这是

我的项目目标的iOS的iPad设备上,但这种精简版瞄准AIR模拟器一个iPad。

我有previously了每一个可用的事件记录和跟踪,但他们从来没有完成任何事情,所以我把他们赶走,为了保持问题简单。

即使我用一个通用的对象,一个字符串属性设置,而不是我的价值目标,它仍然读回(我假设,写出)为

下面是我的一丝回报然后在项目中使用的code:

  [SWF] PictureToolsOnTheMoveMakeItDev.swf  -  2644533字节后DECOM pression
PictureToolsOnTheMoveMakeItDev功能creationCompleteHandler
    file.resolvePath(文件名).nativePath:C:\ Users \用户cepelc \应用程序数据\漫游\ org.PictureTools.Apps.PictureToolsOnTheMoveMakeItDev.debug \本地存储\ User00100 \照片\ 00100-1358359285139.PTotmImageVO
PictureToolsOnTheMoveMakeItDev功能saveImageToLibrary
FileSerializer功能writeObjectToFile()
    FileStream.open(写)TRY
    FileStream.open(写)最后
    FileStream.writeObject(ptotmImageVO)TRY
    FileStream.writeObject(ptotmImageVO)最后
    FileStream.close()
PictureToolsOnTheMoveMakeItDev功能readImageFromLibrary
FileSerializer功能readObjectFromFile(C:\Users\cepelc\AppData\Roaming\org.PictureTools.Apps.PictureToolsOnTheMoveMakeItDev.debug\Local商店\ User00100 \照片\ 00100-1358359285139.PTotmImageVO)
    file.exists:真
    FileStream.open(读)TRY
    FileStream.open(读)最后
    FileStream.readObject()TRY
    FileStream.readObject()最后
    FileStream.close()
    FileSerializer功能readObjectFromFile  -  ptotmImageVO  - 空
    ptotmImageVO:空
[卸载SWF] PictureToolsOnTheMoveMakeItDev.swf
 

下面是我的应用程序MXML:

 < XML版本=1.0编码=UTF-8&GT?;
< S:应用的xmlns:FX =htt​​p://ns.adobe.com/mxml/2009
               XMLNS:S =库://ns.adobe.com/flex/sparkapplicationDPI =240
               的xmlns:C =。组件*
               的creationComplete =creationCompleteHandler()>
    < S:布局>
        < S:VerticalLayout verticalAlign =中间horizo​​ntalAlign =中心/>
    < / S:布局>

    < FX:脚本>
        <![CDATA [
            进口classes.FileSerializer;
            进口vo.PTotmImageVO;

            私人VAR ptotmImageVO:PTotmImageVO;
            私人VAR fileSerializer:FileSerializer =新FileSerializer();
            私人var文件:文件= File.applicationStorageDirectory;
            私人变种文件名:字符串;

            保护功能creationCompleteHandler():无效
            {
                跟踪(PictureToolsOnTheMoveMakeItDev功能creationCompleteHandler);

                ptotmImageVO =新PTotmImageVO();
                ptotmImageVO.userid =00100;
                ptotmImageVO.description =TestPuppyBunnyThingy;
                ptotmImageVO.timestamp =新的日期()的getTime()。
                ptotmImageVO.type =PictureTools  - 在活动中 - 图片实体;

                文件名= ptotmImageVO.userid + - + ptotmImageVO.timestamp +PTotmImageVO。;

                文件= file.resolvePath(User00100);

                如果(file.exists&安培;&安培;!file.isDirectory)
                {
                    file.deleteFile();
                }
                file.createDirectory();

                文件= file.resolvePath(照片);

                如果(file.exists&安培;&安培;!file.isDirectory)
                {
                    file.deleteFile();
                }
                file.createDirectory();

                跟踪(file.resolvePath(文件名).nativePath:+ file.resolvePath(文件名).nativePath);

                saveImageToLibrary();
            } //端功能creationCompleteHandler

            保护功能saveImageToLibrary():无效
            {
                跟踪(PictureToolsOnTheMoveMakeItDev功能saveImageToLibrary);

                fileSerializer.writeObjectToFile(ptotmImageVO,file.resolvePath(文件名).nativePath);

                readImageFromLibrary();
            } //端功能saveImageToLibrary

            保护功能readImageFromLibrary():无效
            {
                跟踪(PictureToolsOnTheMoveMakeItDev功能readImageFromLibrary);

                ptotmImageVO = fileSerializer.readObjectFromFile(file.resolvePath(文件名).nativePath)为PTotmImageVO;
                跟踪(ptotmImageVO:+ ptotmImageVO);
            } //函数结束readImageFromLibrary

        ]]≥
    < / FX:脚本>

< / S:用途>
 

FileSerializer.as类

 包中的类
{
    进口flash.errors.IOError;
    进口flash.filesystem.File将;
    进口flash.filesystem.FileMode;
    进口flash.filesystem.FileStream;

    进口vo.PTotmImageVO;

    公共类FileSerializer
    {
        私人VAR FILESTREAM:的FileStream =新的FileStream();
        私人var文件:文件;

        公共职能FileSerializer()
        {

        } //结束CONSTRUCTOR FileSerializer

        公共职能writeObjectToFile(ptotmImageVO:PTotmImageVO,FNAME:字符串):无效
        {
            跟踪(FileSerializer功能writeObjectToFile());
            文件=新的文件(FNAME);

            尝试
            {
                跟踪(FileStream.open(写)TRY);
                fileStream.open(文件,FileMode.WRITE);
            }
            赶上(五:引发SecurityError)
            {
                跟踪(FileStream.open(写)CATCH引发SecurityError+ E);
            }
            最后
            {
                跟踪(FileStream.open(写入)最后​​);
            }

            尝试
            {
                跟踪(FileStream.writeObject(ptotmImageVO)TRY);
                fileStream.writeObject(ptotmImageVO);
            }
            赶上(五:IO错误)
            {
                跟踪(FileStream.writeObject(ptotmImageVO)CATCH IO错误+ E);
            }
            最后
            {
                跟踪(FileStream.writeObject(ptotmImageVO)终于);
            }

            fileStream.close();
            追踪(FileStream.close());

        } //函数结束writeObjectToFile

        公共职能readObjectFromFile(FNAME:字符串):PTotmImageVO
        {
            迹线(FileSerializer功能readObjectFromFile(+ FNAME +));

            VAR ptotmImageVO:PTotmImageVO;

            文件= file.resolvePath(FNAME);

            跟踪(file.exists:+ file.exists);

            如果(file.exists)
            {
                尝试
                {
                    fileStream.open(文件,FileMode.READ);
                    跟踪(FileStream.open(读)TRY);
                }
                赶上(五:引发SecurityError)
                {
                    跟踪(FileStream.open(读)CATCH引发SecurityError+ E);
                }
                最后
                {
                    跟踪(FileStream.open(读)终于);
                }

                尝试
                {
                    跟踪(FileStream.readObject()TRY);
                    ptotmImageVO = fileStream.readObject()作为PTotmImageVO;
                }
                赶上(五:IO错误)
                {
                    跟踪(FileStream.readObject()CATCH IO错误+ E);
                }
                最后
                {
                    跟踪(FileStream.readObject()终于);
                }

                fileStream.close();
                追踪(FileStream.close());
                跟踪(FileSerializer功能readObjectFromFile  -  ptotmImageVO  - + ptotmImageVO);

                返回ptotmImageVO;
            }
            其他
            {
                返回null;
            }
        } //函数结束readObjectFromFile

    } //末级FileSerializer

} //结束包中的类
 

PTotmImageVO.as值对象

 包VO
{
    进口flash.display.BitmapData;

    [为RemoteClass(别名为PTotmImageVO)

    公共类PTotmImageVO
    {
        公共变种用户名:字符串;
        公共变种缩略图:的BitmapData;
        公共变种形象:的BitmapData;
        公共变种时间戳:数字;
        公共变种描述:字符串;
        公共变种类型:字符串;

        公共职能PTotmImageVO()
        {

        } //结束构造PTotmImageVO
    } //末级PTotmImageVO
} //结束套餐VO
 

解决方案

解决。元数据标签[为RemoteClass ......不是[为RemoteClass作为例如code的显示,我开始无法正常工作。由于编译器不会检查这些标签,并弥补自己的标签是不是技术上的错误,将-NEVER-是从工作的任何诊断数据。

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

解决方案

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 /动作脚本/移动File.writeObject /的readObject总是产生空瓦特/生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆