Exploring Local Android Data Storage
The capability to store data locally on mobile devices is an imperative function of android applications. Local storage is critical for retaining the essential information across several application executions as long as the app is present on your device. Any mobile app without local data storage is made to fetch and display the data straight from the server and loses all its functionality without internet connection. Local android data storage apps remembers and retrieves the recent searches on the app event without an internet connecting. Also, it stores the other important information like application configuration, large amount of information in files and databases, and user preferences to make things easy for app creators.
In order to save persistent app data, there are several options that Android provides you with. Android has three major data storage mechanisms viz. SQLite Database, Preferences or saving key-value sets, and Local Files. In Shared Preferences you can store data using key-value pairs, SQLite databases are file-based databases with .db or .sqlite extensions and Local Files are used with standard java and SQLite preferences. It is important to note that all the shared preferences, databases and files are limited to the app that generated them. This prevents data sharing with other applications, for which you need to get hold of Content Providers.
Local Android Data Storage Using SQLite Database
- SQLite Databases are file-based databases, which are ideal for saving structured data. After you’ve defined the look of your database, the first thing is to implement mechanisms that create SQLite database. Android stores your application’s entire data in a disc space that is not accessible to other apps.
- The data in this private disc is secure because by default this part is inaccessible to other applications on the device.
- To veil complexities of updating, adding or removing any data a SQLiteOpenHelper class is created. This restricts long-running processes of updating and creating database during app startup. In order to use SQLiteOpenHelper class, you need to create a sub-class that overrides some callback methods like onOpen, onCreate and onUpgrade.
- New rows can be inserted into a database by using contentvalues, with each contentvalue object representing a single row.
- To extract over query results Cursor objects can be used as pointers to the result set from a database query. The cursor provides several functions like moveTOFirst, miveToNext, getColoumnName and getCount.
- To delete data from database create a section criteria that identify rows.
Local Android Data Storage Using Shared Preferences
- There are XML files to store set of values in terms of key-value pairs, where a SharesPrefrence object points at the file with key-value pairs. This object provides simple techniques to read and write those key value pairs.
- The data type here refers to floats, ints, longs, Booleans and strings. This is one of the most preferred methods for local data storage
- You can create a new share preference file or get access to the one already existing by calling methods getSharedPreferences AND getPreferences. Special attention must be given while naming the Shared Preference file, as it needs to be uniquely identifiable to the app.
- Next you need to write the Shared Preferences file by creating sharedprefrence.editor, and then pass keys and values you want to write.
- The common location where shared preferences are stored in Android app is /data/data//shared_prefs/
Local Data Storage Using Local Files
- Similar to disc-based file systems on other platforms, android uses a likewise file system. Local files are mainly used to read and write large amounts of data like image files or any heavy files exchanged over a network.
- This system of local android storage is used with standard Java I/O classes and methods. External storage and internal storage are two file storage areas on android devices.
- While saving a file to internal storage you call using of the two methods viz. getFilesDir() and getCacheDir. The first is used to return a file representing internal app directory and the second returns a file representing internal app directory for cache files.
- To save a file to external storage, Android developers can query the external storage for available volume using getExternalStorageState().
These are several methods provided by android to save persistent application data or for local android data storage. However, your choice of method depends upon whether the data should be kept private to your app or should be accessible to other apps as well. The method you choose also depends upon how much space your data will require.
For more information you can REACH OUT TO US HERE.