Internal or External Storage?


Let’s consider where to save data first. Data can be saved internally or externally.

Internally, File.ApplicationDirectory is the directory where your application and its assets are installed. AIR made this directory read-only because it is not writable on all systems. Instead, you can write to File.ApplicationStorageDirectory, the storage directory allocated to your application. Use this location to save fairly small amounts of data and information such as preferences and user settings. For example, your application data should not occupy more than a portion of the application’s full size.

If your application is removed, the data saved in storage is deleted along with it.

Users can erase the data by selecting Settings→Applications→Manage Applications→ Application→Clear Data. They will be alerted to the consequences with a warning that reads, “All of this application’s data will be deleted permanently. This includes all files, settings, accounts, databases and so on.” Android provides the option to set allowClearUserData to false to prevent users from clearing data. At the time of this writing, this feature is not available in the Flash Professional and Flash Builder permissions panel.

Data can also be saved internally in the memory cache allocated to your application. To use this approach, create a temporary file or folder and save data in it. This is a good place to save noncritical information such as downloaded data files that may not have a lasting value, or temporary saved files. If your application is removed, the data saved in the cache is also deleted.

Users can erase the cache under Settings→Applications→Manage Applications→Application→Clear Cache. Android does not provide the option to prevent clearing the cache.

Externally, data can be saved on the device’s SD card under the File.documentsDirectory directory, also referred to as File.userDirectory or File.desktopDirectory. Use this approach for any relatively large amounts of data, such as images or video or temporary files. Create a directory with your application name to keep it distinct from other applications’

Writing to the card requires a permission, which needs to be added to the descriptor file. If you don’t have this permission, AIR will throw a runtime error:

<uses-permission android:name=
“android.permission.WRITE_EXTERNAL_STORAGE” />

Before installing any data, make sure the user’s phone has an SD card:

if (File.userDirectory)
// proceeds with saving data

You can use this approach as a way for one application to write data and another application to access that data.

If your application is deleted, the data is not deleted automatically. However, the data is visible, and can therefore be removed by the user even if your application is not. If the user removes the SD card, the data becomes unavailable.

A word of warning: during development, if you are using Flash Professional to install your application on the device, every uninstall/reinstall deletes previously saved data, including updates. In Flash Builder, you can prevent this behavior from occurring by unchecking the “Clear application data” box on each Launch option when you first create your project. If the user installs an update of your application, however, previously saved data is preserved.

It is better to use filenames than paths to guarantee consistency across devices and platforms, and the resolvePath method to refine the path.

Here is the list of directories and their equivalent paths:

/data/data/app.appID/appID/Local Store
/data/data/app.appID/appID/Local Store

There are several ways to save persistent application data on your device. The amount of space your data requires, and the complexity of the data, will determine which approach to take. We will discuss these options next.