Unit 4: Android Data
Storage Mechanism
Storages in Android
 In Android, there are 2 types of storages are available in android.
    Data storage (Databases, SQLite, Realm, etc..).
    File storage (SD Card, Phone Storage).
 The system provides several options for you to save your app data.
    App-specific storage: Store files that are only used for your application, either in dedicated
     directories within an internal storage or different dedicated directories within external storage.
    Shared Storage: Store files that your app intends to share with other apps, including media,
     documents, and other files.
    Preferences: Store private, primitive data in key-value pairs inside root directories in xml format.
    Databases: Store structured data in a private database using the Room persistence library.
 When storing sensitive data that shouldn't be accessible from any other app use
  internal storage, preferences, or a database.
 Internal storage has the added benefit of the data being hidden from users.
                                                                                                 2
3
4
5
6
7
Shared Preferences
   Shared Preferences can store and retrieve small amounts of data in key/value pairs.
   It can store String, int, float, Boolean in an XML file inside the app.
   It stores data until you clear the preference or uninstall the app from device.
   In order to use shared preferences, you have to call a method
    getSharedPreferences().
               SharedPreferences sharedpreferences = getSharedPreferences(“Settings”,
                                    Context.MODE_PRIVATE);
 The string Settings is the name of the preference file you wish to access If it does not
  exist, it will be created.
 The mode value designates the default behavior which allow read/write access to
  only to the application
 Apart from private there are other modes available
      MODE_PUBLIC: will make the file public which could be accessible by other applications on the
       device.
      MODE_PRIVATE: keeps the files private and secures the user’s data.
      MODE_APPEND: it is used while reading the data from the SP file.
                                                                                             8
Shared Preferences
 If we want to use common preference file and don't wish to specify a file name, we
  can use default shared preferences.
                             SharedPreferences sharedPreferences =
               PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
 Using this way will default the preference file to be stored as
    /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml
 We    can     write  the    data  inside    shared     preference    file using
  SharedPreferences.Editor.
 Once editing has been done, one must commit() or apply() the changes made to the
  file.
                                                                                         9
10
Shared Preferences
 There are difference methods available of shared preferences:
    contains(String key): This method is used to check whether the preferences contain a
     preference.
    edit(): This method is used to create a new Editor for these preferences.
    getAll(): This method is used to retrieve all values from the preferences.
    getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value
     from the preferences.
    getFloat(String key, float defValue): This method is used to retrieve a float value from the
     preferences.
    getInt(String key, int defValue): This method is used to retrieve an int value from the
     preferences.
    getLong(String key, long defValue): This method is used to retrieve a long value from the
     preferences.
    getString(String key, String defValue): This method is used to retrieve a String value from the
     preferences.
    getStringSet(String key, Set defValues): This method is used to retrieve a set of String values
     from the preferences.
                                                                                            11
Internal storage
                   12
Internal storage
                   13
Write/Read data in internal storage
                                      14
External Storage
                   15
16
External storage methods
                           17
What is database
                   18
What is database
                   19
What is database
                   20
SQLite
         21
22
23
24
Sqlite Database in android
                             25
Useful classes for Sqlite
                            26
Create Database`
                   27
Insert in database
                     28
Update in database
                     29
Delete in database
                     30
What is content provider
                           31
32
Content Provider
                   33
Content Provider
                   34
35
36
37