4 MINUTE READ | June 29, 2012
Storing Data On Mobile Platforms: Android
Last week I made a list of the multiple different options for storing persistent data within an iOS Application. This week I thought I’d take a look at some of the different options for data storage within Android OS.
The first option we’ll discuss is the SharedPreferences class. You can use SharedPreferences to store any primitive data type; boolean, float, int, long, or string, and the data will persist across all user sessions. To access the Shared Preferences you’ll want to use one of these two methods; getSharedPreferences(String name, int mode), or getPreferences(int mode). By usinggetSharedPreferences(String name, int mode) you can actually create multiple different preferences files that will each be identified by the name passed in as the first parameter. However,getPreferences(int mode) is actually called on a single Activity, and will create and access just one preferences file linked to that activity.
Saving data to SharedPreferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit();
editor.putString(“StringKey”, “Text to be saved”);editor.putInt(“intKey”, 101); editor.putFloat(“floatKey”, 9.87);
Retrieving data from SharedPreferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String storedString = settings.getString(“StringKey”, null);int storedInt = settings.getInt(“intKey”, 0); float storedFloat = settings.getFloat(“floatKey”, 0.0);
With Android you can actually write files directly into a directory on the device. You can store text documents, images, sound files, movie files, or any other kind of file that you have a means to read, and there are two different ways you can store data like this.
The first way to store data with this method is to use the Internal Storage, which by default stores the files in a directory private to your application, other applications and the user are not allowed access to these files, and when the user uninstalls your application these files are removed along with it.
The other option is to use External Storage, this is a shared storage space that all Android compatible devices are required to support, whether it be a removable storage (an SD Card or the like) or an internal, non-removable storage. Files saved to the External Storage are completely public and can even be accessed via computer. All applications can read and write files placed on the External Storage, and the user can manipulate those files however they see fit. So, this means that any file stored in the External Storage can, at any time, be removed without your application’s knowledge or consent.
Just like iOS, Android has full SQLite support, which is a relational database management system contained in a small C library that is implemented as a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is not a separate process that is accessed from the application, but is actually integrated into it, and is a popular choice as an embedded database for data storage in applications.
SQLite is certainly the best suited option for storing complex model objects, but the set up for all the database connections can make it quite a task to actually implement. Also performing any kind of updates to the table can be rather complicated, and require a full update to the database in live version updates, which, without using the proper precautions, will result in loss of user data obtained prior to the update.
Again, these lists are only meant to be a guide to what your options when trying to figure out what to use to store data in your mobile applications. Which option you choose is entirely up to your needs and preferences. It’s always important to know your options, that way you can ensure that your application is utilizing all of the technology it has access to, to the very best of its ability.
Stay in touch
Subscribe to our newsletter
Post Image by GDS Infographics
Posted by: PMG Advertising Agency
4 MINUTES READ | November 2, 2021
2 MINUTES READ | February 4, 2020
8 MINUTES READ | September 3, 2019
3 MINUTES READ | June 6, 2019
4 MINUTES READ | March 6, 2019