4. Internal Storage
● Private to the application
● Bound to the application on install/uninstall
● No permissions required
● android.content.Context.getFilesDir(): returns a java.io.File object
representing the root directory of the internal storage for your
application from the current context.
5. External Storage
● Visible by all other applications
● READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE
permission required
6. External Storage by device
● Not specific to the application, will stay the same if the application is
uninstalled.
● android.os.Environment.getExternalStorageDirectory(): root directory of
the external storage of the device.
● android.os.Environment.getExternalStoragePublicDirector(directory):
public directory for files of a particular type, like music in
Environment.DIRECTORY_MUSIC or pictures in
Environment.DIRECTORY_PICTURES
7. External Storage by application
● Specific to the application
● Bound to the application on install/uninstall
● android.content.Context.getExternalFilesDir(): get the root directory of
the primary external storage for your application.
● android.content.Context.getExternalFilesDirs(): get the root directories of
all the external storage directories.
8. Preference/SharedPreferences API
● Preference to create a preference activity and SharedPreferences to
access/modify.
● Uses pairs of key-values to represent user preferences
● Can store boolean, float, long, strings and arrays
● Stored in the internal storage
10. Preference API - PreferenceFragment
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences as configured in the xml file
// and displays them.
// The preferences will be automatically saved.
addPreferencesFromResource(R.xml.preferences);
}
}
11. SharedPreferences API
public class MainActivity extends Activity {
@Override
public void onResume() {
super.onResume();
SharedPreferences pref;
pref = PreferenceManager.getDefaultSharedPreferences(this);
// Get the new value from the preferences
TextView welcomeTV;
welcomeTV = (TextView)findViewById(R.id.hello_world_textview);
String welcomeText = preferences.getString("welcome_text", "");
welcomeTV.setText(welcomeText);
}
}
12. SQLite Database
● Relational database for the application with the SQLite engine.
● Stored in the internal storage for the application.
● Needs the SQLiteOpenHelper class:
● onCreate(): event to manage creation of the database.
● onUpdate(): event to manage upgrades to the database.
14. SQLite Database - Creation
public class ProjectsDatabaseHelper extends SQLiteOpenHelper {
// Current database version
public static final int DATABASE_VERSION = 1;
// The name of the database file on the file system
public static final String DATABASE_NAME = "Projects.db";
public ProjectsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create the database to contain the data for the projects
db.execSQL(ProjectContract.SQL_CREATE_TABLE);
db.execSQL(RowCounterContract.SQL_CREATE_TABLE);
initializeExampleData(db);
}
}
15. SQLite Database - Upgrade
public class ProjectsDatabaseHelper extends SQLiteOpenHelper {
[...]
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// Logs that the database is being upgraded
Log.i(ProjectsDatabaseHelper.class.getSimpleName(),
"Upgrading database from version "
+ oldVersion + " to " + newVersion);
}
}
16. SQLite Database - Queries
● Inserting a new row :
SQLiteDatabase db = getWritableDatabase();
db.insert(RowCounterContract.TABLE_NAME,
null /*nullColumnHack*/,
firstProjectCounterContentValues);
● Updating a row :
SQLiteDatabase db = getWritableDatabase();
db.update(RowCounterContract.TABLE_NAME,
currentAmountContentValues,
RowCounterContract.RowCounterEntry._ID +"=?",
new String[] { String.valueOf(rowCounter.getId()) });
17. SQLite Database - Queries
● Deleting a row :
SQLiteDatabase db = getWritableDatabase();
db.delete(RowCounterContract.TABLE_NAME,
RowCounterContract.RowCounterEntry._ID +"=?",
new String[] { String.valueOf(rowCounter.getId()) });
18. SQLite Database - Queries
● Getting data :
ArrayList projects = new ArrayList();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(ProjectContract.TABLE_NAME, null /*columns*/,
null /*selection*/, null /*selectionArgs*/,
null /*groupBy*/, null /*having*/, null /*orderBy*/);
while (cursor.moveToNext()) {
Project project = new Project();
int idColIndex = cursor .getColumnIndex(ProjectContract.ProjectEntry._ID);
long projectId = projCursor.getLong(idColIndex);
project.setId(projCursor.getLong(projectId);
[...]
projects.add(project);
}
projCursor.close();