Android Data
Storage
Android Training
By Khaled Anaqwa
Your data storage options are
the following:
 Shared Preferences
 Store private primitive data in key-value pairs.
 Internal Storage
 Store private data on the device memory.
 External Storage
 Store public data on the shared external storage.
 SQLite Databases
 Store structured data in a private database.
 Network Connection
 Store data on the web with your own network server.
SharedPreferences
 The SharedPreferences class provides a
general framework that allows you to
save and retrieve persistent key-value
pairs of primitive data types.
 This data will persist across user sessions
(even if your application is killed).
To work with shared
preferences you need to
 Initialization
 Storing Data
 Commit changes
 Retrieving Data
 Clearing / Deleting Data
Initialization
Application shared preferences can be fetched
using getSharedPreferences() method.
You also need an editor to edit and save the
changes in shared preferences.
SharedPreferences pref =
getApplicationContext().getSharedPreferences(”FileName", 0); // 0 - for
private mode
Editor editor = pref.edit();
Application shared preferences
can be fetched using
 getSharedPreferences() - Use this if you
need multiple preferences files identified
by name, which you specify with the first
parameter.
 getPreferences() - Use this if you need
only one preferences file for your Activity.
Because this will be the only preferences
file for your Activity, you don't supply a
name.
Storing Data
You can save data into shared preferences using editor. All the
primitive data types like booleans, floats, ints, longs, and strings
are supported.
You need to put data as Key with Value.
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
Commit changes
Call editor.commit() in order to save changes to shared
preferences..
like
editor.commit();
Retrieving Data
Data can be retrived from saved preferences by calling
getString() (For string) method.
Remember this method should be called on Shared Preferences
not on Editor.
// returns stored preference value
// If value is not present return default value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Clearing / Deleting Data
If you want to delete from shared preferences you can call
remove(“key_name”) to delete that particular value.
If you want to delete all the data, call clear().
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes;
editor.clear(); //clear all the data from shared preferences
editor.commit(); // commit changes
Task
 Create Settings with ShaerdPresferance
 General Settings
 Language selection
 Text Size
 Email
 Textcolor
 System Settings
 Enable Notifications
 use Wifi
 About
Using the Internal Storage
 You can save files directly on the device's
internal storage. By default, files saved to
the internal storage are private to your
application and other applications
cannot access them.
 When the user uninstalls your application,
these files are removed.
To create and write a private
file to the internal storage:
 Call openFileOutput() with the name of
the file and the operating mode. This
returns a FileOutputStream.
 Write to the file with write().
 Close the stream with close().
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos =
openFileOutput(FILENAME,Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
To read a file from internal
storage:
 Call openFileInput() and pass it the name
of the file to read. This returns a
FileInputStream.
 Read bytes from the file with read().
 Then close the stream with close().
RAW Files
 If you want to save a static file in your
application at compile time, save the file
in your project res/raw/ directory.
 You can open it with
openRawResource(), passing the
R.raw.<filename> resource ID.
 This method returns an InputStream that
you can use to read the file (but you
cannot write to the original file).
Cache Files:
 If you'd like to cache some data, rather
than store it persistently, you should use
getCacheDir() to open a File that
represents the internal directory where
your application should save temporary
cache files.
PreferenceActivity
 This is the base class for an activity to
show a hierarchy of preferences to the
user.
 this functionality should now be found in
the new PreferenceFragment class.
 using PreferenceActivity in its old mode,
the documentation there applies to the
deprecated APIs here.
Android Training (Storing & Shared Preferences)

Android Training (Storing & Shared Preferences)

  • 1.
  • 2.
    Your data storageoptions are the following:  Shared Preferences  Store private primitive data in key-value pairs.  Internal Storage  Store private data on the device memory.  External Storage  Store public data on the shared external storage.  SQLite Databases  Store structured data in a private database.  Network Connection  Store data on the web with your own network server.
  • 3.
    SharedPreferences  The SharedPreferencesclass provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.  This data will persist across user sessions (even if your application is killed).
  • 4.
    To work withshared preferences you need to  Initialization  Storing Data  Commit changes  Retrieving Data  Clearing / Deleting Data
  • 5.
    Initialization Application shared preferencescan be fetched using getSharedPreferences() method. You also need an editor to edit and save the changes in shared preferences. SharedPreferences pref = getApplicationContext().getSharedPreferences(”FileName", 0); // 0 - for private mode Editor editor = pref.edit();
  • 6.
    Application shared preferences canbe fetched using  getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.  getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
  • 7.
    Storing Data You cansave data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. You need to put data as Key with Value. editor.putBoolean("key_name", true); // Storing boolean - true/false editor.putString("key_name", "string value"); // Storing string editor.putInt("key_name", "int value"); // Storing integer editor.putFloat("key_name", "float value"); // Storing float editor.putLong("key_name", "long value"); // Storing long
  • 8.
    Commit changes Call editor.commit()in order to save changes to shared preferences.. like editor.commit();
  • 9.
    Retrieving Data Data canbe retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor. // returns stored preference value // If value is not present return default value - In this case null pref.getString("key_name", null); // getting String pref.getInt("key_name", null); // getting Integer pref.getFloat("key_name", null); // getting Float pref.getLong("key_name", null); // getting Long pref.getBoolean("key_name", null); // getting boolean
  • 10.
    Clearing / DeletingData If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear(). editor.remove("name"); // will delete key name editor.remove("email"); // will delete key email editor.commit(); // commit changes; editor.clear(); //clear all the data from shared preferences editor.commit(); // commit changes
  • 11.
    Task  Create Settingswith ShaerdPresferance  General Settings  Language selection  Text Size  Email  Textcolor  System Settings  Enable Notifications  use Wifi  About
  • 12.
    Using the InternalStorage  You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them.  When the user uninstalls your application, these files are removed.
  • 13.
    To create andwrite a private file to the internal storage:  Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.  Write to the file with write().  Close the stream with close().
  • 14.
    String FILENAME ="hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();
  • 15.
    To read afile from internal storage:  Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.  Read bytes from the file with read().  Then close the stream with close().
  • 16.
    RAW Files  Ifyou want to save a static file in your application at compile time, save the file in your project res/raw/ directory.  You can open it with openRawResource(), passing the R.raw.<filename> resource ID.  This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
  • 17.
    Cache Files:  Ifyou'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files.
  • 18.
    PreferenceActivity  This isthe base class for an activity to show a hierarchy of preferences to the user.  this functionality should now be found in the new PreferenceFragment class.  using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here.