Everything About Storage on Android by Cindy Potvin ( @CindyPtn) for DroidCon Montreal 2015 http://blog.cindypotvin.com
Everything About Storage on Android
by Cindy Potvin (@CindyPtn)for DroidCon Montreal 2015
http://blog.cindypotvin.com
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.
External Storage● Visible by all other applications
● READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission required
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
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.
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
Preference API - PreferenceFragment<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" ><EditTextPreference android:key="welcome_text" android:title="@string/pref_title_welcome_text" android:summary="@string/pref_summary_welcome_text" android:defaultValue="@string/pref_default_welcome_text" /><ListPreference android:key="welcome_text_color" android:title="@string/pref_title_welcome_text_color" android:summary="@string/pref_summary_welcome_text_color" android:defaultValue="@string/pref_default_welcome_text_color" android:entries="@array/colorLabelsArray" android:entryValues="@array/colorValuesArray" /></PreferenceScreen>
Preference API - PreferenceFragmentpublic 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); }}
SharedPreferences APIpublic class MainActivity extends Activity {@Overridepublic 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); }}
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.
SQLite Database - Creationpublic 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); } }
SQLite Database - Upgradepublic 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); } }
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()) });
SQLite Database - Queries● Deleting a row : SQLiteDatabase db = getWritableDatabase(); db.delete(RowCounterContract.TABLE_NAME, RowCounterContract.RowCounterEntry._ID +"=?", new String[] { String.valueOf(rowCounter.getId()) });
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();