Skip to main content
Known Participant
September 22, 2012
Question

applicationStorageDirectory on Android behaviour

  • September 22, 2012
  • 3 replies
  • 1889 views

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....

This topic has been closed for replies.

3 replies

Colin Holgate
Inspiring
September 22, 2012

Did you set the permissions for writing to internal and external storage?

Known Participant
September 22, 2012

External storage permisison is set by default, but i never seen something about internal permissions....

note that, the first time the app starts, it successfuly create the file on android devices.

Yes maybe my post is incomplete , here is what happens on android devices :

1st launch:

- file is created, and the file is detected as "existing"

2nd launch:

- file is not detected as "existing", and is creating again

And the most weird is that the file seems to be created in app as the filesize of the app is increasing according to Android settings info in Storage tab.

(i saw this bug because in the first place, i was storing big files, and they were loading again on each app start on Android...)

Known Participant
September 22, 2012

Ok, i may have an answer to this...

It seems that installing a new version of the app (after each compilation) resets the app-storage data.

It works on my android device when i :

1) start the app from eclipse in debug mode

2) kill the app process in android settings

3) launch the app again

(If I recompile from eclipse again, the data disappear again).

By experience, when replacing an existing app always kept the storage data... (and this is the case on IOS / desktop versions)

So is this a glitch on android app update ?