[go: up one dir, main page]

0% found this document useful (0 votes)
65 views2 pages

07 Handout 1

Internal storage and external storage are options for data storage in Android. Internal storage is private to the app and saves files locally, while external storage can be accessed by other apps and saves files publicly. Shared preferences provide a simple way to store private primitive data in key-value pairs. SQLite databases store structured data in tables with rows and columns and support basic CRUD operations. The SQLite database is self-contained, serverless, and transactional.
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)
65 views2 pages

07 Handout 1

Internal storage and external storage are options for data storage in Android. Internal storage is private to the app and saves files locally, while external storage can be accessed by other apps and saves files publicly. Shared preferences provide a simple way to store private primitive data in key-value pairs. SQLite databases store structured data in tables with rows and columns and support basic CRUD operations. The SQLite database is self-contained, serverless, and transactional.
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/ 2

IT1918

Data Storage • A file can be created in either of the two (2) directories:
o Permanent storage: The getFilesDir() method returns
Data Storage Options the absolute path to the directory on the filesystem where
• The data storage options in Android are as follows: files created are stored.
o Shared preferences – key-value pairs that store private o Temporary storage: The getCacheDir() method returns
primitive data. A key-value pair (KVP) is a set of two (2) the absolute path to the application-specific cache directory
linked data items: a key, the unique identifier for some item on the filesystem. This is recommended for small,
of data, and the value, the data that is identified. temporary files totaling less than 1MB. The system will
o Internal storage – device memory where private data is automatically delete files in the cache directory as disk
stored space is needed elsewhere on the device.
o External storage – stores public data • To create a file:
o SQLite databases – private databases used for storing File myFile = new File(context.getFilesDir(), filename);
structured data
o Room persistence library – caches an SQLite database • To write to a file in the internal directory:
locally, and automatically syncs changes to a network String filename = "myfile";
database String string = "Writing!";
o Cloud backup – saves user data in the cloud FileOutputStream outputStream;
o Firebase realtime database – stores and syncs data with
a NoSQL cloud database. Data is synced across all clients try {
in real time and remains available when your app goes outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
offline. outputStream.write(string.getBytes());
o Custom data store – stores preferences in a specific outputStream.close();
storage location using the Preferences API. This library } catch (Exception e) {
manages the user interface and interacts with storage so e.printStackTrace();
that you define only the individual settings that the user can }
configure.
• To access the external storage, indicate a uses-permission
Internal Storage and External Storage attribute in the Android manifest.
• The table below summarizes the comparison between internal • To write to the external storage:
storage and external storage. <uses-permission
Internal Storage External Storage android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Always available Not always available • To read (without writing) the external storage:
The files of an app cannot be Can be accessed by any app <uses-permission
accessed by other apps. android:name="android.permission.READ_EXTERNAL_STORAGE" />
The app's files are removed The app's files can be removed
once the app is uninstalled. using getExternalFilesDir(). • To delete a file: myFile.delete();
It is best to use when you do It is best to use if you want to
not want other users or other share files with other users or
apps to access your files. other apps.

07 Handout 1 *Property of STI


 student.feedback@sti.edu Page 1 of 2
IT1918
Shared Preferences • To delete all the key-value pairs:
• Only one (1) shared preferences file is needed for an app. SharedPreferences.Editor editor = mPreferences.edit();
• The shared preferences file is named with the package name of the editor.clear();
app. editor.apply();
• The shared preferences file should be in the onCreate() method
of the app's main activity and stored in a member variable.
SQLite Databases
• To create a shared preferences file:
• SQLite is one of the most widely deployed database engines in the
private String sharedPrefFile = "com.example.android.myapplication"; world. The source code for SQLite is in the public domain. It
mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE); implements an SQL database engine that has the following
• To save a shared preferences file: characteristics:
1. Get a SharedPreferences.Editor, the interface used for o Self-contained (requires no other components)
modifying values in a SharedPreferences object. o Serverless (requires no server backend)
2. Add key/value pairs to the editor using the "put" method o Zero-configuration (does not need to be configured for your
appropriate for the data type. Some of the methods are app)
putInt(), putString(), putFloat, and putBoolean(). o Transactional (changes within a single transaction in
3. Call apply() to commit the changes. The apply() method SQLite either occur completely or not at all)
performs the requested modifications, replacing whatever • The four (4) basic database operations are inserting rows, deleting
is currently in the shared preferences file. rows, updating values in rows, and retrieving rows that meet given
int x = 143; criteria.
SharedPreferences.Editor editor = mPreferences.edit(); • The SQLiteDatabase class has methods to execute SQL
editor.putInt("num", x); commands and perform other common database management
editor.putString("text", "demo"), tasks.
editor.apply(); • To create a database:
SQLiteDatabase db;
• Use the "get" methods to retrieve the value in the shared db=openOrCreateDatabase("EmployeeDB", Context.MODE_PRIVATE, null);
preferences file. Some of the methods are getInt(),
getString(), getFloat, and getBoolean(). These methods • To execute an SQL statement:
take two (2) arguments, one for the key and one for the default value db.execSQL("CREATE TABLE IF NOT EXISTS student(stdnt_id VARCHAR,
if the key cannot be found. stdnt_name VARCHAR, stdnt_prog VARCHAR);");
• To retrieve a shared preferences file: • The rawQuery() method runs the provided SQL and returns a
mPreferences = getSharedPreferences(sharedPrefFile, Cursor of the result set. A cursor is a pointer to a table row.
MODE_PRIVATE); Cursor c = db.rawQuery("SELECT * FROM student WHERE stdnt_id='"+
mNum = mPreferences.getInt("num", 0); etStdntID.getText()+"'", null);
mText = mPreferences.getString("text", null);
• To delete a specific key-value pair:
SharedPreferences.Editor editor = mPreferences.edit(); References:
DiMarzio, J. (2017). Beginning Android programming with Android Studio. Indiana: John Wiley
editor.remove("num"); & Sons, Inc.
editor.apply(); Google Developers Training Team. (2018). Android developer fundamentals (version 2).
Retrieved from https://google-developer-training.github.io

07 Handout 1 *Property of STI


 student.feedback@sti.edu Page 2 of 2

You might also like