0% found this document useful (0 votes)
39 views18 pages

CH 4

This document discusses storing and retrieving data in mobile applications. It covers several options for data storage in Android like shared preferences, internal storage, external storage, SQLite databases, and network connections. It provides details on how to use each of these storage options, including code examples for shared preferences and accessing internal and external storage. The objectives are to explain data storage and retrieval in mobile apps and understand synchronization, replication, and working with content providers.

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views18 pages

CH 4

This document discusses storing and retrieving data in mobile applications. It covers several options for data storage in Android like shared preferences, internal storage, external storage, SQLite databases, and network connections. It provides details on how to use each of these storage options, including code examples for shared preferences and accessing internal and external storage. The objectives are to explain data storage and retrieval in mobile apps and understand synchronization, replication, and working with content providers.

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Jimma University

JIT Mobile Application


Faculty of computing development
&Informatics

Chapter Four
Storing and Retrieving Data

1
Chapter Objectives
At the end the chapter students should able to know:

 • Explain how to store and retrieving data using mobile

application.

 Describe Synchronization and Replication of Mobile Data

 Understand working with a content provide

2
Data Storage in Android
 Android provides several option to save persistent
application data

 The solution you choose depends on your specific needs


 whether the data should be private to your application

 or accessible to other applications (and the user) and

 how much space your data requires

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

primitive data types.

 You can use SharedPreferences to save any primitive data:


booleans, floats, ints, longs, and strings.

 This data will persist across user sessions (even if your


application is killed).

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.

 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
1. Shared Preference
 To write values:
 Call edit()to get a SharedPreferences.Editor.

 Add values with methods such as putBoolean()and putString().

 Commit the new values with commit()

 To read values, use SharedPreferences methods such as


 getBoolean()and

 getString().

8
Demo

9
2. 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 (nor can the user).

 When the user uninstalls your application, these files are


removed.

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.

2. Write to the file with write().

3. Close the stream with close().

 Ex: String FILENAME = "hello_file";

String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

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.

 Files saved to the external storage are world-readable and can be

modified by the user when they enable USB mass storage to transfer files

on a computer.

 Checking media availability:

 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/

 The <package_name>is your Java-style package name, such as


“com.example.android.app“

14
Saving files that should be shared
 If you want to save files that are not specific to your

application and that should not be deleted when your


application is uninstalled, save them to one of the public
directories on the external storage.

 These directories lay at the root of the external storage,


such as Music/, Pictures/, Ringtones/, and others.

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

You might also like