Saturday, July 12, 2014

Android - Saving Data

The principal file storage options in Android are the ones below 

- Saving Key value pairs of simple data types in preference file.
- Saving Arbitrary files in Android file system. 
- Using Database managed by SQlite 

Key value pair saving can be via SharedPreferences API. Internally, system keeps a file to store the key value pairs. This shared preference can be made a private or shared. 

Below is a code snippet to invoke the SharePreference call from a Fragment. The context of Fragment is Activity 

Context context = getActivty(); 
SharedPreferences preferences = context.getSharedPreferences(getString(R.strings.preference_file_key),Context.MODE_PRIVATE);

While naming the preference file, it is better to name with the reverse DNS name. For e.g. com.example.PREFERENCE_FILE_KEY 

Alternatively, if application needs only one preference file, then getPreferences cane be called like below 

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

If the Application specifies the preference file name access as MODE_WORLD_READABLE, MODE_WORLD_WRITABLE, it will be accessible by other application if the preference file name is know to those apps. 

Below is the code to write to SharedPreferences = getActivity().getPreferences()

SharedPreferences preferences = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor = preferences.edit();
editor.putInt("high_score",newhighscore);
eidtor.commit();

While reading the preference, we can give an optional value that will get returned if the value for the key we are reading is not present. 

Saving Files
There are two storage areas: "internal" and "external" storage representing built in memory and SD card respectively. 

Internal storage is always accessible. Files stored here is accessible only by the application by default. When the user uninstalls the app, the stored file also get deleted. 
External storage is not always accessible because user can unmount the card. By default the data stored here is world readable. When app is uninstalled, system will remove the apps files only if the saving is done in the getExternalFilesDir()/ 

If the application needs to get installed on to file system, then that can be specified in the android manifest file. 

IF the application would like to write to external storage, a permission needs to be requested in the manifest file via the property android.permission.READ_EXTERNAL_STORAGE / WRITE_EXTERNAL_STORAGE. 

To get applications root files directory, getFilesDir api can be called. To get the caches directory, getCachesDir api can be called. Caches directory is a temporary directory. When there is any memory constraint situation arises, system will delete files from this directory without any warning. 

When trying to access external storage, always should try to see if it is mounted. There are APIs available such as getExternalStorageState which will return Environment.MEDIA_MOUNTED value. Application can check if the media is read only by checking the value as MEDIA_MOUNTED_READ_ONLY 

The files can be stored in the external directory in two forms 

Public : Files are freely available for other applications, the public directory can be obtained by using the API Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
Private : Even though files are accessible to the user, they don't make much sense to the user. the API getExternalFilesDir can be used for this. Both these APIs accept parameter such as Environment.DIRECTORY_PICTURES so that the system categoryzes the files properly. 

The API getFreeSpace() or getTotalSpace() can be called to know the freespace and the total space. The recommendation is that if the storage is 90% full, then don't write anything. Also, application can write without checking the size and gets into IOEXception, then handle it accordingly. 


When user uninstalls the App, Android system does the following 

- Delete all files in internal storage
- Delete all field on external storage that used getExternalFilesDir() 

Application should delete all the files created with getCacheDir on a regular basis if no longer needed. 

No comments:

Post a Comment