Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Everything about storage - DroidconMtl 2015

666 views

Published on

My presentation at DroidCon Montreal 2015 about storage on Android (internal and external storage, preferences API and SQLite database)

Published in: Software
  • Be the first to like this

Everything about storage - DroidconMtl 2015

  1. 1. Everything About Storage on Android   by Cindy Potvin (@CindyPtn) for DroidCon Montreal 2015 http://blog.cindypotvin.com
  2. 2. Storage hardware ● Varies depending on the device :  Internal memory  SD card
  3. 3. 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.
  4. 4. External Storage ● Visible by all other applications ● READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission required
  5. 5. 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
  6. 6. 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.
  7. 7. 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
  8. 8. 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>
  9. 9. 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); } }
  10. 10. 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); } }
  11. 11. 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.
  12. 12. SQLite Database - Example
  13. 13. 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); } }
  14. 14. 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); } }
  15. 15. 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()) });
  16. 16. SQLite Database - Queries ● Deleting a row : SQLiteDatabase db = getWritableDatabase(); db.delete(RowCounterContract.TABLE_NAME, RowCounterContract.RowCounterEntry._ID +"=?", new String[] { String.valueOf(rowCounter.getId()) });
  17. 17. 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();
  18. 18. Life Cycle - When to save
  19. 19. Life Cycle - When to restore
  20. 20. Thank you!   @CindyPtn http://blog.cindypotvin.com

×