Top Banner
Everything About Storage on Android by Cindy Potvin ( @CindyPtn) for DroidCon Montreal 2015 http://blog.cindypotvin.com
21

Everything about storage - DroidconMtl 2015

Jul 18, 2015

Download

Software

Cindy Potvin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Everything about storage - DroidconMtl 2015

Everything About Storage on Android

by Cindy Potvin (@CindyPtn)for DroidCon Montreal 2015

http://blog.cindypotvin.com

Page 2: Everything about storage - DroidconMtl 2015
Page 3: Everything about storage - DroidconMtl 2015

Storage hardware● Varies depending on the device :

Internal memory SD card

Page 4: Everything about storage - DroidconMtl 2015

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.

Page 5: Everything about storage - DroidconMtl 2015

External Storage● Visible by all other applications

● READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission required

Page 6: Everything about storage - DroidconMtl 2015

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

Page 7: Everything about storage - DroidconMtl 2015

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.

Page 8: Everything about storage - DroidconMtl 2015

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

Page 9: Everything about storage - DroidconMtl 2015

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>

Page 10: Everything about storage - DroidconMtl 2015

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); }}

Page 11: Everything about storage - DroidconMtl 2015

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); }}

Page 12: Everything about storage - DroidconMtl 2015

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.

Page 13: Everything about storage - DroidconMtl 2015

SQLite Database - Example

Page 14: Everything about storage - DroidconMtl 2015

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); } }

Page 15: Everything about storage - DroidconMtl 2015

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); } }

Page 16: Everything about storage - DroidconMtl 2015

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()) });

Page 17: Everything about storage - DroidconMtl 2015

SQLite Database - Queries● Deleting a row : SQLiteDatabase db = getWritableDatabase(); db.delete(RowCounterContract.TABLE_NAME, RowCounterContract.RowCounterEntry._ID +"=?", new String[] { String.valueOf(rowCounter.getId()) });

Page 18: Everything about storage - DroidconMtl 2015

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();

Page 19: Everything about storage - DroidconMtl 2015

Life Cycle - When to save

Page 20: Everything about storage - DroidconMtl 2015

Life Cycle - When to restore

Page 21: Everything about storage - DroidconMtl 2015

Thank you!

@CindyPtn http://blog.cindypotvin.com