Flex Mobile: SharedObject on SD Card

Posted: August 31, 2011 in Flex Mobile

In Texas, we have a lot of Oil and Gas Facilities both onshore and offshore.  They leak Green House Gases (GHG). One of my clients provides tools for testing and reducing GHG.  I built a Flex Mobile tablet application for them to take into the field.  It stores data offline since they don’t always have an internet connection.

Challenge was: What do you do if you drop the tablet on the rig and it smashes?  Do you lose all the test data captured on the tablet?  The solution I came up with is to copy the offline data stored on the device onto the SD card.  This would allow them to be able to move the data onto their backup tablet and continue on with the testing using the new device.

This function copies the Local SharedObject, that stores the offline data, onto the SD Card.


public function soToSD():void {

var soOnSD:File = File.documentsDirectory.resolvePath("mGHG.sol");

var soDir:String = File.applicationStorageDirectory.resolvePath("#SharedObjects").nativePath;

var soInAppStorage:File = new File(soDir);

soInAppStorage = soInAppStorage.resolvePath('mGHG2.swf/mGHG.sol');

soInAppStorage.copyTo(soOnSD, true);

var files:Array = File.documentsDirectory.getDirectoryListing();

for (var i:uint = 0; i < files.length; i++) {

trace(files[i].nativePath);

}

}

Advertisements
Comments
  1. Laurence says:

    Seems like a nice solution but unfortunately am having issues implementing on Android. Does this method still work for you? Says location does not exist for the local sharedobject.

  2. DoubleTake says:

    Laurence, this worked for me on Android prior to Honeycomb 3.2. It took away the ability to write externally all together. You can no longer grant write permissions to the true external SD Card or USB. So, I had to abandon copying to the SD Card. I believe it still works using the mounted SD Card, which isn’t a real external card.

    But, the soInAppStorage path should still work for locating the SharedObject on Android. Make sure you replace my sample path with your own app’s path. (mGHG2.swf, etc.). This is the name in your app-xml file.

    Don

  3. Laurence says:

    Indeed I had replaced the SWF and SO name. I’m thinking that ‘soInAppStorage.exists’ should trace ‘true’ but I am getting ‘false’. I tried this to confirm the ‘#SharedObjects’ directory and ‘sol’ file, but received false on both.

    • DoubleTake says:

      soInAppStorage.exists should return true.
      Are you setting the SO with getLocal() first?
      e.g.
      offlineSO = SharedObject.getLocal(soName);
      soDir = File.applicationStorageDirectory.resolvePath(“#SharedObjects”).nativePath;
      soInAppStorage = new File(soDir);

      Don
      soInAppStorage = soInAppStorage.resolvePath(soPath);

  4. Laurence says:

    yep…

    temp = File.documentsDirectory.resolvePath(“MyApp”);
    mySo = SharedObject.getLocal(“MyApp.sol”,”/”);
    soOnSD = temp.resolvePath(“MyApp.sol”);
    soDir = File.applicationStorageDirectory.resolvePath(“#SharedObjects/MyApp.swf”).nativePath;
    soInAppStorage = new File(soDir);
    trace(“soInAppStorage:” + soInAppStorage.exists); //false
    soInAppStorage = soInAppStorage.resolvePath(“MyApp.sol”);
    trace(“soInAppStorage:” + soInAppStorage.exists); //false
    soInAppStorage.copyTo(soOnSD,true); // Error #3003: File or directory does not exist.

  5. DoubleTake says:

    Only difference I see is that you are using the local path “/” in getLocal() and you are including .sol in your SO’s name. I don’t do that and it works for me. I just use getLocal(‘myApp’) .

    the path and the .sol won’t exist until it is created, so it seems yours is not getting created. The rest of your code looks like it should work.

    Don

  6. Laurence says:

    OK, so…

    Checked when I was using the sol extension and used it only when using File class and made sure the SO name was all lowercase and no numbers and seems to work! Thanks so much for your help. Took me all day, but only 18 minutes overtime. 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s