Professional Documents
Culture Documents
Data Storage: Universitatea Tehnică "Gh. Asachi" Din Iaşi
Data Storage: Universitatea Tehnică "Gh. Asachi" Din Iaşi
Data storage
2021
Data storage
Contents
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
2. Shared Preferences
3. Internal Storage
4. External Storage
5. SQL Database
Data storage
1. Types of data storages
❖Android system provides many options for application developers for storing data.
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
– In Internal storage: Store application files in a private area that it's not accessible
to other applications
– In External storage: Store files in an area that it's accessible to other applications
in the context of an application. Thus, the data is accessible only to the application that created
it. Android device stores each app’s Shared Preferences inside of an XML file in a private
directory.
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
“Pref_Name” is the identifier of the location where the data is be stored. Thus, in the
application, the data is visible to any Activity that obtained the instance to the
2 MODE_ENABLE_WRITE_AHEAD_LOGGING
Database open flag. When it is set , it would enable write ahead logging by default
3 MODE_MULTI_PROCESS
This method will check for modification of preferences even if the sharedpreference
instance has already been loaded
4 MODE_PRIVATE
By setting this mode, the file can only be accessed using calling application
5 MODE_WORLD_READABLE
This mode allow other application to read the preferences
6 MODE_WORLD_WRITEABLE
This mode allow other application to write the preferences
Data storage
2. Shared Preferences
❖The key is of type String
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
❖The value can be of type int, long, boolean, float, String or String Set.
application, which is not accessible to other applications. The user can have access to that
❖ Example of writing data to a file that is saved private for that application in the Internal
storage:
//Function openFileOutput returns a FileOutputStream object to the file to
write.
//”aTextFile.txt” represents the name of the file
//Assigning Context.MODE_PRIVATE makes the file to be private for the
application
FileOutputStream fOutStream = openFileOutput("aTextFile.txt",
Context.MODE_PRIVATE);
//write the content to the file
fOutStream.write(content.getBytes());
//close the FileOutputStream object
fOutStream.close();
Data storage
3. Internal storage
❖Example of reading from file from the Internal storage:
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
❖External storage is typically represented by a SDCard and it's available at the /sdcard
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
path.
– Public files: Files that should be accessible to other apps and to the user. These
– Private files: These files are stored in application’s specific directory and are
external storage
❖Example:
<manifest ...>
...
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
Data storage
4. External storage
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
❖In order to check if the external storage is available for reading and writing, the
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
In order to check if the external storage is available for reading, the following verification
should be done:
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Data storage
External Storage
❖Example of writing a private file to the External storage:
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
if(isExternalStorageWritable()) {
//getExternalFilesDir returns the allocated dir for the app
for a specific type of files
File fileNote = new
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
File(getApplicationContext().getExternalFilesDir(Environment.DIREC
TORY_DOWNLOADS), "myExternalFile.txt");
//obtain a FileOutputStream for the file
FileOutputStream fOutStream = new FileOutputStream(fileNote);
//write the content to the file
fOutStream.write(content.getBytes());
//close the FileOutputStream object
fOutStream.close();
}
else
{
System.out.println("Storage is not writable");
}
Data storage
4. External Storage
❖Example of reading a private file from the External storage:
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
if(isExternalStorageReadable()) {
//Obtain a inputStream to the file
File fileNote = new
File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNL
OADS), "myExternalFile.txt");
FileInputStream fInStream = new FileInputStream(fileNote);
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
if (null != fInStream) {
//Obtain an inputStreamReader from the inputStream
InputStreamReader fInputStreamReader = new
InputStreamReader(fInStream);
//obtain a bufferreader
BufferedReader buffReader = new BufferedReader(fInputStreamReader);
//read line by line the content of the file
String newLine = buffReader.readLine();
while (newLine != null) {
content.append(newLine).append('\n');
newLine = buffReader.readLine();
}
}
}
else {
System.out.println("Storage is not readable");
}
Data storage
5. SQL Database
❖Android allows storing data in an SQL database, which is the SQLite database.
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
❖For accessing the SQLite database, it’s needed to create a class extends the class
❖Example of such class, which creates a database that has a table with two columns:
public class LoginDB extends SQLiteOpenHelper {
//The name of the database file
private static final String LOGIN_DATABASE_NAME = "login.db";
//the name of the table
private static final String TABLE_NAME = "accounts";
//the name of the user column in the table
private static final String FIELD_USER = "user";
//the name of the password column in the table
private static final String FIELD_PASSWORD = "password";
//the version of the database
private static final int DATABASE_VERSION = 1;
LoginDB class. The method will be called by the system when creating the database:
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
/*
SQL instruction for creating the table accounts, which has two
columns, user and password.
Column user is primary key for the table, which means that its values
must be unique.
*/
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME +
" ("+ FIELD_USER + " TEXT PRIMARY KEY, " +
FIELD_PASSWORD + " TEXT);");
}
Data storage
5. SQL Database
❖The OnUpgrade() method of the SQLiteOpenHelper class must be overridden in the
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
LoginDB class. The method will be called by the system when the structure of the
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
}
Data storage
5. SQL Database
❖ For inserting data into the “accounts” table, a dedicated method can be added to the
FACULTATEA DE ELECTRONICĂ, TELECOMUNICAŢII ŞI TEHNOLOGIA INFORMAŢIEI
Example of a method that queries for the rows that have the column “user” equal to the
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
LoginDB class.
This is an example of a method that deletes the rows that have the column “user” equal
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
added to the LoginDB class. Example of a method that updates the password on the row
that has the column “user” equal to the user given as parameter
UNIVERSITATEA TEHNICĂ “GH. ASACHI” DIN IAŞI
internal and external storage is best for storing files such as music, videos,
and documents, while SQLite wins if you need to perform fast searches and
❖Ultimately, the storage method you choose should depend on your data
types, the length of time you need the data, and how private you want the
data to be.