Search code examples
androidactionscript-3apache-flexfile-ioflex4.5

Check if LOCAL file exists (Flex 4 Mobile)


I'm currently using this code:

        private function getFile(file:String):void
        {
            var openFile:URLRequest = new URLRequest("file:///sdcard/GNs/"+file);
            try {navigateToURL(openFile);}
            catch(e:Error)
            {
                var download:URLRequest = new URLRequest("http://"+file);
                new DLAlert().open(this, true);
            }
        }

to find a file, and give a popup for an optional download if it does not exist. The problem is, the errors returned when trying the navigateToURL(...) command are handled by the web/file browser, and not by the app. Is there a way to look for it without trying to open it?


Solution

  • Use this:

    var myfile:File = new File("/sdcard/GNs/"+file);
    if (myfile.exists)
    {
       // your code here
    }
    else
    {
       // your code here
    }