CH 4
CH 4
Chapter Four
Storing and Retrieving Data
1
Chapter Objectives
At the end the chapter students should able to know:
application.
2
Data Storage in Android
Android provides several option to save persistent
application data
3
Data Storage in Android
1. Shared Preferences
2. Internal Storage
3. External Storage
4. SQLite Databases
5. Network
Connection
4
Data Storage in Android
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.
5
1. Shared Preference
The SharedPreferences class provides a general framework
that allows you to save and retrieve persistent key-value pairs of
6
1. Shared Preference
To get a SharedPreferences object for your application,
use one of two methods:
getSharedPreferences() – Use this if you need multiple
preferences files identified by name, which you specify with the
first parameter.
7
1. Shared Preference
To write values:
Call edit()to get a SharedPreferences.Editor.
getString().
8
Demo
9
2. Using the Internal Storage
You can save files directly on the device’s internal storage.
10
To create and write a private file to the internal storage:
1. Call openFileOutput()with the name of the file and the
operating mode. This returns a FileOutputStream.
fos.write(string.getBytes());
fos.close();
11
Using the External Storage
Every Android-compatible device supports a shared “external storage” that
you can use to save files.
This can be a removable storage media (such as an SD card) or an internal
(non-removable) storage.
modified by the user when they enable USB mass storage to transfer files
on a computer.
Before you do any work with the external storage, you should always call
getExternalStorageState() to check whether the media is available
12
Accessing files on external storage:
If you’re using API Level 8 or greater, use getExternalFilesDir() to open a File
that represents the external storage directory where you should save your files.
This method takes a type parameter that specifies the type of subdirectory you
want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to
receive the root of your application’s file directory).
This method will create the appropriate directory if necessary. By specifying the
type of directory, you ensure that the Android’s media scanner will properly
categorize your files in the system (for example, ringtones are identified as
ringtones and not music).
If the user uninstalls your application, this directory and all its contents will be
deleted.
13
Accessing files on external storage:
If you’re using API Level 7 or lower, use
getExternalStorageDirectory(), to open a File representing the
root of the external storage. You should then write your data
in the following directory:
/Android/data/<package_name>/files/
14
Saving files that should be shared
If you want to save files that are not specific to your
15
Using Databases
Android provides full support for SQLite databases.
Any databases you create will be accessible by name to
any class in the application, but not outside the
application.
The recommended method to create a new SQLite
database is to create a subclass of SQLiteOpenHelper
and override the onCreate() method, in which you can
execute a SQLite command to create tables in the
database.
16
Demo
17
18