Search code examples
androidactionscript-3mobileairsaving-data

Where is the best location to save in Android mobile using AIR and how


Where is the best location to save in Android mobile using AIR and how? Im trying to figure out where i can save my file. Im creating a MEMO App where the user can save his/her File, my question is, where is the best location in Android Mobile so save?


Solution

  • That is what File.applicationStorageDirectory is for.

    You can save whatever data you want to the file (XML, JSON, AMF, sqlite database), and you can save asynchronously to avoid UI hangs.

    Example saving string data (such as serialized XML or JSON):

    function save(data:String):void {
        var file:File = File.applicationStorageDirectory.resolvePath("preferences.data");
        var fileStream:FileStream = new FileStream();
        fileStream.open(file, FileMode.WRITE); // or openAsync()
        fileStream.writeUTFBytes(data);
        fileStream.close();
    }
    
    function load():String {
        var file:File = File.applicationStorageDirectory.resolvePath("preferences.data");
        var fileStream:FileStream = new FileStream();
        fileStream.open(file, FileMode.READ);
        var data:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
        fileStream.close();
        return data;
    }