1 Reply Latest reply on Jan 11, 2011 1:25 PM by aktell2007

    Retrieving file from ‘applicationStorageDirectory’ ....

    aktell2007 Level 1

       

       

      I have this Load Manager Package class file now for a while and I would like to make changes to it e.g. 1st step. ‘How to read the Xml/Text file’.

       

      The first part is the original code.

       

      package com.components.KnowYourArtists.Internal {
           
           import com.components.KnowYourArtists.CustomItems.KnowItems;
           import com.components.KnowYourArtists.DataLayer.XmlData;
           
           import flash.events.Event;
           import mx.core.FlexGlobals;
           
           public class LoadManager {
                
                private static var _xmlData : XmlData = null;           
                private static var _nextFilename : String = null;
                private static var _knowItems: KnowItems = null;
                
                public static function Start():void {
                     
                     FlexGlobals.topLevelApplication.addEventListener(Event.ENTER_FRAME, enterFrame);
                     load("com/components/KnowYourArtists/data/dataItemType-knowItems.txt");
                }
                
                public static function load(filename:String) : void {
                     _nextFilename = filename;
                }
                
                private static function enterFrame(e:Event):void {     
                     
                     if ( _xmlData == null && _nextFilename != null ) {
                          _xmlData = new XmlData(_nextFilename);
                          _nextFilename = null;
                     }
                     
                     if ( _xmlData != null && !_xmlData.hasFinished ) {
                          trace("put loading animation here if necessary");
                     }
                     
                     if ( _xmlData != null && _xmlData.hasFinished ) {
                          //set main data object
                          FlexGlobals.topLevelApplication.knowItems = new KnowItems(_xmlData.getContents());                    
                          //set first one
                          FlexGlobals.topLevelApplication.list.selectedItem = FlexGlobals.topLevelApplication.knowItems.getCollection()[0];                    
                          //set it so it stops loading     
                          _xmlData = null;
                     }               
                }          
           }
      }
      

       

      The second part is in a way how I would like to do the same as above, but loading/reading it from the 'applicationStorageDirectory'. I have left two lines in there which are not used just for testing things. The App works as is eliminating all Errors, but of course with nothing showing of the text file.

       

      The text file is very simple here are two line of it:

      One; Two; Three; Four; dummy

      Five; Six; Seven; Eight; dummy

      .... ongoing

       

      package com.components.KnowYourArtists.Internal {
           
           import com.components.KnowYourArtists.CustomItems.KnowItems;
           import com.components.KnowYourArtists.DataLayer.XmlData;
           
           import flash.events.Event;
           import flash.filesystem.*;
           
           import mx.core.FlexGlobals;
           
           public class LoadManager {
                
                private static var _xmlData : XmlData = null;           
                private static var _nextFilename : String = null;
                private static var _knowItems: KnowItems = null;
                
                public static function Start():void {
                     
                     FlexGlobals.topLevelApplication.addEventListener(Event.ENTER_FRAME, enterFrame);
                     /*load("com/components/KnowYourArtists/data/dataItemType-knowItems.txt");*/
                     
                     var myFile:File = File.applicationStorageDirectory.resolvePath("txtFile/dataItemType-knowItems.txt");
                     var fileStream:FileStream = new FileStream();
                     
                     fileStream.open(myFile, FileMode.READ);
                     /*var str:String = fileStream.readUTFBytes(fileStream.bytesAvailable);*/
                     fileStream.close();
                     
                     /*str = str.replace(File.lineEnding, "\n");*/
                }
                
                public static function load(filename:String) : void {
                     _nextFilename = filename;
                }
                
                private static function enterFrame(e:Event):void {     
      
                     if ( _xmlData == null && _nextFilename != null ) {
                          _xmlData = new XmlData(_nextFilename);
                          _nextFilename = null;
                     }
      
                     if ( _xmlData != null && !_xmlData.hasFinished ) {
                          trace("put loading animation here if necessary");
                     }
      
                     if ( _xmlData != null && _xmlData.hasFinished ) {
                          //set main data object
                          FlexGlobals.topLevelApplication.knowItems = new KnowItems(_xmlData.getContents());                    
                          //set first one
                          FlexGlobals.topLevelApplication.list.selectedItem = FlexGlobals.topLevelApplication.knowItems.getCollection()[0];                    
                          //set it so it stops loading     
                          _xmlData = null;
                     }               
                }          
           }
      }
      

       

      Thanks in advance for any help or ideas ! regards aktell2007