保存并从Android移动设备使用Flash ActionScript 3.0中加载文件并从、加载、文件、设备

2023-09-09 21:40:36 作者:思念〃熬成灰

林做一个测试设备,应用为桌面和Android。我能够保存和加载桌面,但未能在Android上。我只能救我的文件,但我不能加载我保存的文件。而Android设备提示没有找到文件。它甚至犯规打开文件浏览器。保存和载入,即时通讯做的类型是用户实际上可以找他的项目并打开它的类型。即时通讯目前使用的FileReference做到这一点。它适用于台式机,但不能在Android手机。有人可以帮我吗?

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?

推荐答案

我是pretty的肯定你不能使用的FileReference 移动(好像有人随时纠正我,如果错了)。

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

在移动,你应该使用文件&放大器; 的FileStream 类加载/保存本地文件。

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();

有除了 writeUTF 其他保存方法(这是纯文本)的writeObject 好保存自定义对象当与 flash.net.registerClassAlias​​

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

文件类在 flash.filesystem

修改

下面是一些你可以尝试让用户选择AIR上的位置。

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();
}