I tried to test blow code on Android device but i couldn't see any data in text fields. This code works well on AIR Desktop but in Android no. Is it difference between AIR FileStream function on Desktop and Android? Whats best cross platform code to save files and read write?
This code works on Adobe Animate CC Android Emulator.
import flash.filesystem.*;
var prefsFile:File;
[Bindable] var prefsXML:XML;
var stream:FileStream;
function appStart():void
{
prefsFile = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
readXML();
}
function readXML():void
{
stream = new FileStream();
if (prefsFile.exists) {
stream.open(prefsFile, FileMode.READ);
processXMLData();
}
else
{
saveData();
}
}
function processXMLData():void
{
prefsXML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
trace(prefsXML.Data1);
trace(prefsXML.Data2);
trace(prefsXML.Data3);
txt_D1.text = prefsXML.Data1;
txt_D2.text = prefsXML.Data2;
txt_D3.text = prefsXML.Data3;
}
function windowClosingHandler(event:Event):void
{
saveData();
}
function saveData():void
{
createXMLData();
writeXMLData();
}
function createXMLData():void
{
prefsXML = <preferences/>;
prefsXML.Data1 = 1;
prefsXML.Data2 = 2;
prefsXML.Data3 = 3;
}
function writeXMLData():void
{
var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
outputString += prefsXML.toXMLString();
outputString = outputString.replace(/\n/g, File.lineEnding);
stream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.writeUTFBytes(outputString);
stream.close();
}
appStart();
That's the correct way, we use it for AIR app (Desktop, Android, iOS) to store user data on device/PC, File.applicationStorageDirectory
as well. So your code is cross-platform, if it does not work on Android, you probably have some other issue, but your code looks fine.
To read/write data to File.applicationStorageDirectory
you don't need any explicit permissions in app manifest as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With