applicationStorageDirectory on Android behaviour
Hello,
I found a strange behaviour while using the "applicationStorageDirectory" in order to store some data in my app.
Here is my code, you can paste it in a blan AIR/mobile project :
package {
import flash.text.TextField;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
public class testStorage extends Sprite {
// file to store in application storage
public var filename : String = "test.txt";
// file content as array
public var fileContent : String;
// debug textfield
public var debug_tf : TextField;
// constructor
public function testStorage() {
// build debuf textfield
debug_tf = new TextField();
debug_tf.width = stage.fullScreenWidth;
debug_tf.height = stage.fullScreenHeight;
debug_tf.textColor = 0xAAAAAA;
addChild(debug_tf);
// first time app starts:
// -> the file does not exists, create it with a string
// -> write the file into application storage
// -> check that the file is created
// second time the app starts:
// -> the file should exists, and then is simply loaded
// -> its content is shown
trc("-------- Test if file exists -----------");
if ( testFileExistence() ){
trc("-> file exists, just load its content ");
loadFile();
} else {
trc("-> file doe not exists, create a new one.");
writeFile();
trc("-------- Verify that file exists -----------");
testFileExistence();
}
}
// load file from applicationStorage
private function loadFile( ):void{
var myFile : File = File.applicationStorageDirectory.resolvePath(filename);
var fileStream : FileStream = new FileStream();
fileStream.open( myFile, FileMode.READ);
fileContent = fileStream.readObject();
fileStream.close();
trc("check file content -> " + fileContent);
}
// create a new file and populate it with a random array (100 elements)
private function writeFile( ):void{
var myFile : File = File.applicationStorageDirectory.resolvePath(filename);
// create content
fileContent = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt nunc eu ante egestas faucibus";
// write into filestream
var fileStream : FileStream = new FileStream();
fileStream.open( myFile, FileMode.WRITE );
fileStream.writeObject( fileContent );
fileStream.close();
trc("-> file created and written in application storage ");
}
// test if file exists
private function testFileExistence():Boolean {
var myFile : File = File.applicationStorageDirectory.resolvePath(filename);
trc("file exists : " + myFile.exists );
if (myFile.exists) trc(" filesize= "+myFile.size);
return myFile.exists;
}
// trace function
private function trc( ...args ):void{
debug_tf.appendText(args.toString() + "\n");
}
}
}
This example simply try to create a file in app-storage if it does exists, and check its creation.
Next time the app is started, it simply loaded the file and check its content.
The fact is this exemple successfully runs on :
- eclipse/FDT when running Android Simulator
- eclipse/FDT when running IOS Simulator
- on all ios devices (tested iphone 4, ipad2 & ipad3)
BUT it does not work on android devices :
- my folio100 tablet
- android AVD simulator
I use Eclipse / FDT5, under adobe Flex SDK 4.6 + AIR 3.4
I really don't get it....
