Virtually every non-trivial application will have to store data in one way or another. This data can be of different forms, such as user settings, application settings, user data, images, or a cache of data fetched from the internet.
Some apps might generate data that ultimately belongs to the user, and so, would prefer to store the data (perhaps documents or media) in a public place that the user can access at anytime, using other apps. Other apps might want to store data, but do not want this data to be read by other apps (or even the user).
The Android platform provides developers with multiple ways to store data, with each method having it’s advantages and disadvantages. Ways to store data :There are basically four different ways to store data in an Android app:
- Shared Preferences
You should use this to save primitive data in key-value pairs. In this method, you have a key, which must be a String. And then there’s the corresponding value for that key, which can be one of: boolean, float, int, long or string.
The Android platform stores an app’s Shared Preferences in an xml file in a private directory. Apps can have multiple Shared Preferences files. Ideally, you will want to use Shared preferences to store application preferences.
- Internal Storage
In a lot of situations, Shared Preferences is too limiting if you want to persist data. You may want to persist Java objects, or images. Or your data logically needs to be persisted using the familiar filesystem hierarchy.
The Internal Storage method is specifically for those situations where you need to store data to the device filesystem, but you do not want any other app (even the user) to read this data. Data stored using the Internal Storage method is completely private to your application, and are deleted from the device when your app is uninstalled.
- External Storage
Conversely, there are other instances where you might want the user to view the files and data saved by your app, if they wish. To save (and/or read) files to the device’s external storage, your app must request for the WRITE_EXTERNAL_STORAGE permission. If you only want to read from the External Storage without writing, request for the READ_EXTERNAL_STORAGE permission.
The WRITE_EXTERNAL_STORAGE permission grants both read/write access. However, beginning with Android 4.4, you can actually write to a “private” external storage folder without requesting WRITE_EXTERNAL_STORAGE. The “private” folder can be read by other applications and the user but the data stored in these folders are not scanned by the media scanner.
This app_private folder is located in the Android/data directory, and is also deleted when your app is uninstalled. Beginning with Android 7.0, apps can request for access to a particular directory, rather than requesting for access to the entire external storage. This way, your app can, for example, request access to either the pictures directory only, or the document directory. This is referred to as scoped directory access.
- SQLite database
Finally, Android provides support for apps to use SQLite databases for data storage. Databases created are app specific, and are available to any class within the app, but not to outside applications.
It goes without saying, that before you decide to use an SQLite database for data storage in your app, you should have some SQL knowledge. Our Android Mobile App experts know SQL along with everything else that can make your app perfect. Get your app made Today!