8 Replies Latest reply on Oct 1, 2008 5:30 PM by Joe ... Ward

    Accessing External Files in AIR

    walker6o9
      Is there a way, in AIR, to access files that are not packaged with the AIR file, but exist in the same folder. Currently I have an AIR file that needs to pull in some xml files and some images. The images are quite large (some are 100 MB's), so I would prefer if they didn't all have to be set as include files.
        • 1. Re: Accessing External Files in AIR
          Oliver Goldman Adobe Employee
          There's no way to get files in the same folder as your application unless they're part of the AIR file. However, there are plenty of writable locations where you could write these files to and access them there. Of course, if you don't include them in the AIR file, you'll need to arrange some other method for getting them on the machine.

          • 2. Re: Accessing External Files in AIR
            walker6o9 Level 1
            I'm creating a CD-ROM, so if I burn the files to the DVD as well as the AIR file, can I access them with AIR. And if so, how?
            • 3. Re: Accessing External Files in AIR
              MarinTodorov
              create a new file reference

              file1 = new air.File();

              and use the nativePath property to point to the files on the cdrom :

              file1.nativePath = "/mounts/CDROM/..../myFile.jpg ";

              • 4. Re: Accessing External Files in AIR
                walker6o9 Level 1
                This is my current loadFile function and my current write file function at the bottom. How would I use the new file reference to amend my code, I am little confused?

                public function loadFile():void
                {
                var filename:String = this.fileList[this.currentFile];

                var byteLoader:URLLoader = new URLLoader();

                byteLoader.dataFormat = URLLoaderDataFormat.BINARY;
                byteLoader.addEventListener( Event.COMPLETE, onDownloaded );
                byteLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
                byteLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
                byteLoader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );

                var fileRequest = new URLRequest( filename );

                try
                {
                byteLoader.load( fileRequest );
                }
                catch (error:Error)
                {
                this.showErrorScreen();
                }
                }










                public function loadFile():void
                {
                var filename:String = this.fileList[this.currentFile];
                var byteLoader:URLLoader = new URLLoader();

                byteLoader.dataFormat = URLLoaderDataFormat.BINARY;
                byteLoader.addEventListener( Event.COMPLETE, onDownloaded );
                byteLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
                byteLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
                byteLoader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );

                var fileRequest = new URLRequest( filename );

                try
                {
                byteLoader.load( fileRequest );
                }
                catch (error:Error)
                {
                this.showErrorScreen();
                }
                }

                // On file downloaded, save file
                public function onDownloaded(evt:Event)
                {
                var filename:String = this.fileList[this.currentFile];

                saveFile( file.nativePath + "/" + filename, evt );

                this.currentFile++;

                if ( this.currentFile < this.fileList.length )
                {
                loadFile();
                }
                else
                {
                showCompleteScreen();
                }
                }

                //--------------------------------------------------------------------------------
                // Write file to hard drive
                public function saveFile( filename:String, evt:Event )
                {
                //filename = "bg13.jpg";
                filename = filename.substr(filename.lastIndexOf("/") + 1);



                var myFile:File = File.documentsDirectory.resolvePath(filename);
                trace("myFile: " + myFile.nativePath);
                var myFileStream:FileStream = new FileStream();

                myFileStream.open( myFile, FileMode.WRITE );
                myFileStream.writeBytes( evt.target.data, 0, evt.target.data );

                }
                • 5. Re: Accessing External Files in AIR
                  walker6o9 Level 1
                  Oops, I copied and pasted my loadFile() function twice.
                  • 6. Re: Accessing External Files in AIR
                    walker6o9 Level 1
                    Oops, I copied and pasted my loadFile() function twice.
                    • 7. Re: Accessing External Files in AIR
                      walker6o9 Level 1
                      I tried this, but it didn't work:

                      //--------------------------------------------------------------------------------
                      public function loadFile():void
                      {
                      var filename:String = this.fileList[this.currentFile];
                      var byteLoader:URLLoader = new URLLoader();

                      byteLoader.dataFormat = URLLoaderDataFormat.BINARY;
                      byteLoader.addEventListener( Event.COMPLETE, onDownloaded );
                      byteLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
                      byteLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
                      byteLoader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );

                      file1 = new air.File();
                      file1.nativePath = "/Users/freelance9/Desktop/080916_Interfaces/Interfaces/ToolkitDisk1/test.jpg";

                      var fileRequest = new URLRequest( file1 );

                      try
                      {
                      byteLoader.load( fileRequest );
                      }
                      catch (error:Error)
                      {
                      this.showErrorScreen();
                      }
                      }
                      • 8. Re: Accessing External Files in AIR
                        Joe ... Ward Level 4
                        URLRequest takes a URL string, not a File object in the constructor.

                        Also, see http://www.adobe.com/devnet/air/flex/quickstart/xml_prefs.html for a simple example that reads and writes a file.