Game Development Reference
In-Depth Information
Storing application data
An application should be able to save its temporary and persistent data. Sometimes data
should be written into a folder on external storage accessible by other applications. Let's ind
out how to get the path to this folder on Android and Windows, and do this in a portable way.
Getting ready
If your Android smartphone unmounts its external storage while connected to a desktop
computer, make sure you disconnect it and wait for the storage to be remounted.
How to do it...
1.
We need to write some Java code to accomplish this task. First, we will ask the
Environment for the external storage directory and its sufix, so we can distinguish
our data from other applications:
protected String GetDefaultExternalStoragePrefix()
{
String Suffix = "/external_sd/Android/data/";
return Environment.getExternalStorageDirectory().getPath() +
Suffix + getApplication().getPackageName();
}
The Suffix value can be chosen at will. You can use whatever
value you desire.
2.
This is quite simple; however, we have to perform some additional checks to make
sure this path is really there. On some devices, for example, without external storage,
it will be unavailable.
String ExternalStoragePrefix = GetDefaultExternalStoragePrefix();
String state = Environment.getExternalStorageState();
3.
Check, if the storage is mounted and can be written to:
if ( !Environment.MEDIA_MOUNTED.equals( state ) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) )
{
ExternalStoragePrefix = this.getDir(
getApplication().getPackageName(), MODE_PRIVATE
).getPath();
}
 
Search WWH ::




Custom Search