Saving and Loading files from an Android mobile Device using Flash Actionscript 3.0

心已入冬 提交于 2019-12-18 07:25:07

问题


Im making a Test Maker App for both Desktop and Android. I was able to save and load in Desktop but failed on Android. I can ONLY save my file, but i cannot load my saved file. The Android device says "No files were found". It doesnt even open a file browser. The type of saving and loading that im doing is the type where the user can actually look for his project and open it. Im currently using FileReference to do this. It works on Desktop but not on Android Mobile. Can somebody help me with this?


回答1:


I'm pretty sure you can't use a FileReference on mobile (though someone feel free to correct me if wrong).

On mobile, you should use the File & FileStream classes for loading/saving local files.

Here is an example, of using a compile time constant you could create (to determine if desktop or AIR) and loading appropriately into and out of a textfield called textfield:

CONFIG::air {
    var file:File = File.applicationStorageDirectory.resolvePath("myFile.txt");

    if(file.exists){
        var stream:FileStream = new FileStream();
        stream.open(file, FileMode.READ);

        textfield.text = stream.readUTF(); //this may need to changed depending what kind of file data you're reading

        stream.close();
    }else{
        data = "default value"; //file doesn't exist yet
    }
}

CONFIG::desktop {
    //use your file reference code to populate the data variable
}

And to save: (assuming you have a textfield whose text you want to save)

var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTF(textfield.text);
fileStream.close();

There are other save methods besides writeUTF (which is for plain text) writeObject is good for saving custom object when combined with flash.net.registerClassAlias

File classes are in the flash.filesystem package

EDIT

Here is something you can try for letting the user pick the location on AIR.

var file:File = File.applicationStorageDirectory;
file.addEventListener(Event.SELECT, onFileSelected);
file.browseForOpen("Open Your File", [new FileFilter("Text Files", "*.txt")]);

function onFileSelected(e:Event):void {
    var stream:FileStream = new FileStream();
    stream.open(e.target as File, FileMode.READ);

    textField.text = stream.readUTF();
}

var saveFile:File = File.applicationStorageDirectory.resolvePath("myFile.txt");
saveFile.addEventListener(Event.SELECT, onSaveSelect);
saveFile.browseForSave("Save Your File");

function onSaveSelect(e:Event):void {
    var file:File = e.target as File;

    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.WRITE);
    stream.writeUTF(textField.text);
    stream.close();
}


来源:https://stackoverflow.com/questions/29513604/saving-and-loading-files-from-an-android-mobile-device-using-flash-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!