AS3 / AIR, Generate a .txt file by pressing the save button without dialog box

1.2k views Asked by At

This is a Flash application to be deployed in Android device thru Adobe Air. I am trying to save the username and score (boxTwo.text + _clickTxt.text) of the user in a notepad .txt file, without any dialog box appearing in android device. It will be generated once the save button (btnSave) is pressed. I can't make it work. Thanks! This is my code:

import flash.net.FileReference;
import flash.events.Event;

var so:SharedObject = SharedObject.getLocal("Test");
var f:File=new File("path\to\file.txt")
var str:FileStream=new FileStream();

btnSave.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
    so.data.saveData = currentFrame;
    so.flush();

}

btnSave.addEventListener (MouseEvent.CLICK, saveFile)
function saveFile(evt):void
{
    str.open(f, FileMode.WRITE);
    str.writeUTFBytes(boxTwo.text + _clickTxt.text);
    str.close();
}
1

There are 1 answers

0
Alexandr Mostovoy On

By Default Adobe Air does not allow to write files to any directory You can get writing access only to:

  • File.documentsDirectory
  • File.applicationStorageDirectory
  • a directory that was been chosen by "browseForDirectory()" dialog box

    fileVariable.browseForDirectory("Choose a directory");

(There are other directories specific for each platform but generally they are these 3 types) So just change your path to an allowed directory

    var string:String = "Text";
    var file:File = File.documentsDirectory.resolvePath("myTxtFile.txt");
    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.WRITE);
    stream.writeUTFBytes(string);
    stream.close();