You are on page 1of 3

Data Manipulation

Using Shared Preferences


For data storage, the SharedPreferences framework in Android allows you to
store primitive data types such as int, float, and boolean, as key-value pairs. The
storage is sandboxed (isolated) relative to your app and isnt visible to other applications
(including those that access your storage). This arrangement makes
SharedPreferences ideal for storing a handful of user settings. Information stored in
SharedPreferences persists between sessions, so users dont have to keep changing
settings every time they open the app. The first item to specify is a String value for the
name of your SharedPreferences file.

Saving Form Data with SharedPreferences

You can use the persistent storage mechanism called SharedPreferences to


store the application settings. Using these preferences, you can save all the form values
on the settings screen.

Define SharedPreferences Entries:

private SharedPreferences myPreference;

Defining a SharedPreferences member variable:


Within the onCreate() method of the activity, initialize this member variable as
follows:

myPreference = getSharedPreferences(prefName,Context.MODE_PRIVATE);

Now, any time you need to save a preference within your application, simply open a
SharedPreferences.Editor, assign a specific preference setting, and commit the
change.

Example:
Editor editor = myPreference.edit();
editor.putString(keyName,keyValue );
editor.commit();

myPreference.contains(keyname)
myPrefernce.getString(keyname)

Accessing Application Preferences


Shared application preferences can be shared using the getSharedPreferences()
method of the application context. Use the SharedPreferences class to save simple
application data, such as configuration settings. Each SharedPreferences object can be
given a name, which organize preferences into categories or store preferences all
together in one large set. For Example:
SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "Spunky");
prefEditor.putBoolean("HasCredits", true);
prefEditor.commit();

To retrieve preference settings, simply retrieve SharedPreferences and read the values
back out:

SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);


String userName = settings.getString("UserName", "Chippy Jr. (Default)");

Working with Raw Files

An application can include raw files as resources. Raw files in application might
use include audio files, video files, and any other file formats you might need. All raw
resource files should be included in the /res/raw resource directory. All raw file
resources must have unique names, excluding the file suffix (meaning that file1.txt and
file1.dat would conflict). To access a raw file resource programmatically from within your
Activity class, simply use the openRawResource() method of the Resources class.

Example:
InputStream iFile = getResources().openRawResource(R.raw.file1);

There are times when you might want to include files within your application but
not have them compiled into application resources. Android provides a special project
directory called /assets for this purpose. This project directory resides at the same level
as the /res directory. Any files included in this directory are included as binary
resources, along with the application installation package, and are not compiled into the
application.
Uncompiled files, called application assets, are not accessible through the
getResources() method. Instead, you must use AssetManager to access files included
in the /assets directory.

Example:

InputStream iFile = assetManager.open(filename);

Working with Files

Each Android application has its own private directory on the Android file system
for storing application files. In addition to all the familiar File ( java.io.File ) and Stream
( java.io.Stream ) classes available, you can access private application files and
directories by using the following Context class methods: fileList() , getFilesDir() ,
getDir() , openFileInput() , openFileOutput() , deleteFile() , and getFileStreamPath() .
These features can be helpful if your application needs to generate files or download
them from the Internet. Now that the help.xml layout file is complete, the
QuizHelpActivity class must be updated to read the quizhelp.txt file and place the
resulting text into the TextView control called helpText.

Adding Raw Resource Files

Raw resource files, such as the quizhelp.txt text file, are added to a project by simply
including them in the /raw resources project directory. This can be done by creating
them as a new file, dragging them in from a file-management tool, or any other way
youre accustomed to adding files to Android projects in Eclipse.

Accessing Raw File Resources


The Android platform includes many of the typical Java file I/O classes, including
stream operations. To read string data from a file, use the openRawResource() method
of the Resources class from within your activity, as in the following example:
InputStream helpFileStream = getResources().openRawResource(R.raw.quizhelp);
Now that you have an InputStream object, you can read the file, line-by-line or byte-by-
byte, and create a string. There are a number of ways to do this in Java. Heres a simple
Java method that reads an InputStream and returns a String with its contents:

public String inputStreamToString(InputStream is) throws IOException {


StringBuffer sBuffer = new StringBuffer();
DataInputStream dataIO = new DataInputStream(is);
String strLine = null;
while ((strLine = dataIO.readLine()) != null) {
sBuffer.append(strLine + "\n");
}
dataIO.close();
is.close();
return sBuffer.toString();
}

You might also like