5 Replies Latest reply on Jan 28, 2010 10:23 AM by heavyboots

    Cannot access local files after converting from FLASH to AIR

    Philip Hozier

      HI,

      This is my first attempt at making a desktop AIR application using FLASH. My problem is that i can access XML files from the local hard drive when developing in FLASH,  but when i publish it in AIR and install the  app it is unable to find the files on the local harddrive.

       

      I'm using code like:

      var whichfile:String = "myfile.xml"

      var file:File = new File();

      file = File.applicationDirectory.resolvePath(whichfile);

      or

      file.url = "app:/"+whichfile;

       

      whith the myfile.xml sitting right next to the application.

       

      Anybody with any thoughts why this would happen.I don't believe it's a sandbox issue as the files are never being downloaded at anypoint or from another domain.

      Am i making a "noob mistake"?

       

      Many thanks in advance.

        • 1. Re: Cannot access local files after converting from FLASH to AIR
          heavyboots Level 2

          If it works when you're running from Flash but not from the installed app, my first impulse is to suspect some sort of permissions issues are involved.

           

          What if you change the directory you're trying to access it in to the documentsDirectory instead?

          • 2. Re: Cannot access local files after converting from FLASH to AIR
            Philip Hozier Level 1

            I've tried using the code for the desktop instead, with the file it wants to access on the desktop too without any success.

            I also tried "including" the files within the Air Application settings when publishing to AIR. The application can then see the files and work. But i can't change the files because they're within the application.

             

            Is there some sort of code for permissions i need to add to allow access outside the application so i can read external files/folders?

            • 3. Re: Cannot access local files after converting from FLASH to AIR
              heavyboots Level 2

              I recommended the approach I did because I vaguely recall something similar-sounding going awry when I first tried to do AIR file loading/saving.

               

              Beyond that, all I can say is here is some working code that I've cadged from the AIR manual and adapted slightly.

               

               

              var loadFile = new File();
              var txtFilter:FileFilter = new FileFilter("Text", "*.xml");
              loadFile = File.documentsDirectory;
              
              try {
                loadFile.browseForOpen("Please choose the XML file to load your highlights from:", [txtFilter]);
                loadFile.addEventListener(Event.SELECT, loadData);
              } catch (error) {
                trace("Failed:"+error.message);
              }
              
              function loadData(event):void  {
              
                 var stream:FileStream = new FileStream();
                 stream.open(event.target, FileMode.READ);
                 myXML = XML(stream.readUTFBytes(stream.bytesAvailable));
                 trace('xml loaded');
                 trace(myXML);
              }
              


              EDIT: At the very least, you could trace the loadFile.url after loading it...

              • 4. Re: Cannot access local files after converting from FLASH to AIR
                Philip Hozier Level 1

                Great that worked like a charm both in Flash and as an AIr app. At least i know now that my computer is capable of making AIR apps. Now to findout what your code is doing that mine isn't...??

                 

                var XMLloaded:String = "";

                 

                        private function LoadFile (which:String):void


                        {
                      
                            var file = new File();
                            file = File.applicationDirectory.resolvePath(which);
                            // create stream
                            var stream = new FileStream();
                            // add listeners
                            stream.addEventListener (ProgressEvent.PROGRESS, readInProgress, false, 0, true);
                            stream.addEventListener (Event.COMPLETE, readComplete, false, 0, true);
                            stream.addEventListener (IOErrorEvent.IO_ERROR, readError, false, 0, true);

                           // start

                           stream.openAsync (file, FileMode.READ);

                 

                        }
                        private function readInProgress (e:Event):void
                        {
                                  XMLloaded += e.target.readMultiByte(e.target.bytesAvailable,File.systemCharset);
                         }
                        private function readComplete (e:Event):void
                        {
                        
                            e.target.close ();
                            // remove listeners
                            e.target.removeEventListener (ProgressEvent.PROGRESS, readInProgress);
                            e.target.removeEventListener (Event.COMPLETE, readComplete);
                            e.target.removeEventListener (IOErrorEvent.IO_ERROR, readError);

                         }

                 

                 

                I tried loading the file syncronously as per your example, however this just stopped(froze) the whole "app" without any output from the intended downloaded xml file.

                 

                Does anything look out of place?? Many thanks in advance.

                 

                p.s. I tried your code looking at "File.applicationDirectory" instead as an AIR app. What i was lead to believe (by several books) was that was the pointer to the directory that the application was  installed in. What i've found out is that it is actually looking WITHIN the application at the file system! But it works when testing  from within Flash accessing xml files outside the SWF.

                 

                Looks like i will need to try and resolve the path to the files i want form the documentsDirectory or systemDirectory.

                • 5. Re: Cannot access local files after converting from FLASH to AIR
                  heavyboots Level 2

                  Well on my machine your code appears to be working correctly--at least from Flash and from the standpoint of checking results via a trace(XMLloaded) statement after the e.target.close statement... I also changed the location to documentsDirectory again for my test, but it did work correctly from there.

                   

                  (SA compiles are problematic here so I can't test that.)