SlideShare a Scribd company logo
1 of 21
Download to read offline
Everything About Storage on Android
 
by Cindy Potvin (@CindyPtn)
for DroidCon Montreal 2015
http://blog.cindypotvin.com
Storage hardware
● Varies depending on the device :
 Internal memory
 SD card
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 - 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);
}
}
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);
}
}
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 - Example
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);
}
}
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);
}
}
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();
Life Cycle - When to save
Life Cycle - When to restore
Thank you!
 
@CindyPtn
http://blog.cindypotvin.com

More Related Content

What's hot

Android datastorage
Android datastorageAndroid datastorage
Android datastorageKrazy Koder
 
Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9Eclipse Day India
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?DicodingEvent
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Khaled Anaqwa
 

What's hot (8)

Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 
Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 
Indexed db
Indexed dbIndexed db
Indexed db
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)
 

Viewers also liked

Sample sample paper of class 10th sa2
Sample sample paper of class 10th sa2Sample sample paper of class 10th sa2
Sample sample paper of class 10th sa2NIpun Chopra
 
Tugas 2 (Teori Organisais Umum 2)
Tugas 2 (Teori Organisais Umum 2)Tugas 2 (Teori Organisais Umum 2)
Tugas 2 (Teori Organisais Umum 2)briant_1995
 
Sharing thoughts on our Joined Project
Sharing thoughts on our Joined ProjectSharing thoughts on our Joined Project
Sharing thoughts on our Joined Projectvaliasazak
 
Rock' n' roll simple steps for children
Rock' n' roll simple steps for childrenRock' n' roll simple steps for children
Rock' n' roll simple steps for childrenvaliasazak
 
This is our school
This is our schoolThis is our school
This is our schoolvaliasazak
 

Viewers also liked (12)

Sample sample paper of class 10th sa2
Sample sample paper of class 10th sa2Sample sample paper of class 10th sa2
Sample sample paper of class 10th sa2
 
Tugas 2 (Teori Organisais Umum 2)
Tugas 2 (Teori Organisais Umum 2)Tugas 2 (Teori Organisais Umum 2)
Tugas 2 (Teori Organisais Umum 2)
 
044805 new1
044805 new1044805 new1
044805 new1
 
R 20120307 SPS WC
R 20120307 SPS WCR 20120307 SPS WC
R 20120307 SPS WC
 
R 20091105 AlN APL
R 20091105 AlN APLR 20091105 AlN APL
R 20091105 AlN APL
 
SE535684C2 (1)
SE535684C2 (1)SE535684C2 (1)
SE535684C2 (1)
 
Resume_Pavan
Resume_PavanResume_Pavan
Resume_Pavan
 
Sharing thoughts on our Joined Project
Sharing thoughts on our Joined ProjectSharing thoughts on our Joined Project
Sharing thoughts on our Joined Project
 
Rock' n' roll simple steps for children
Rock' n' roll simple steps for childrenRock' n' roll simple steps for children
Rock' n' roll simple steps for children
 
This is our school
This is our schoolThis is our school
This is our school
 
R 20050122 Si ejmp
R 20050122 Si ejmpR 20050122 Si ejmp
R 20050122 Si ejmp
 
Barrett’s esophagus
Barrett’s esophagusBarrett’s esophagus
Barrett’s esophagus
 

Similar to Everything about storage - DroidconMtl 2015

Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Hassan Abid
 
The Best Way to Become an Android Developer Expert with Android Jetpack
The Best Way to Become an Android Developer Expert  with Android JetpackThe Best Way to Become an Android Developer Expert  with Android Jetpack
The Best Way to Become an Android Developer Expert with Android JetpackAhmad Arif Faizin
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 StorageDiego Grancini
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptxFaezNasir
 
Android, Gradle & Dependecies
Android, Gradle & DependeciesAndroid, Gradle & Dependecies
Android, Gradle & DependeciesÉdipo Souza
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence libraryThao Huynh Quang
 
Google app-engine-with-python
Google app-engine-with-pythonGoogle app-engine-with-python
Google app-engine-with-pythonDeepak Garg
 
"Android" mobilių programėlių kūrimo įvadas #3
"Android" mobilių programėlių kūrimo įvadas #3"Android" mobilių programėlių kūrimo įvadas #3
"Android" mobilių programėlių kūrimo įvadas #3Tadas Jurelevičius
 
Third-party App Stores on Android.pptx.pdf
Third-party App Stores on Android.pptx.pdfThird-party App Stores on Android.pptx.pdf
Third-party App Stores on Android.pptx.pdfAayush Gupta
 
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...Peter Martin
 
Building Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsBuilding Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsHassan Abid
 
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdfPertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdfHendroGunawan8
 
Solid fire cloudstack storage overview - CloudStack European User Group
Solid fire cloudstack storage overview - CloudStack European User GroupSolid fire cloudstack storage overview - CloudStack European User Group
Solid fire cloudstack storage overview - CloudStack European User GroupShapeBlue
 
CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire NetApp
 

Similar to Everything about storage - DroidconMtl 2015 (20)

Android-data storage in android-chapter21
Android-data storage in android-chapter21Android-data storage in android-chapter21
Android-data storage in android-chapter21
 
Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018
 
The Best Way to Become an Android Developer Expert with Android Jetpack
The Best Way to Become an Android Developer Expert  with Android JetpackThe Best Way to Become an Android Developer Expert  with Android Jetpack
The Best Way to Become an Android Developer Expert with Android Jetpack
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 Storage
 
Save data in to sqlite
Save data in to sqliteSave data in to sqlite
Save data in to sqlite
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx
 
Android, Gradle & Dependecies
Android, Gradle & DependeciesAndroid, Gradle & Dependecies
Android, Gradle & Dependecies
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence library
 
Google app-engine-with-python
Google app-engine-with-pythonGoogle app-engine-with-python
Google app-engine-with-python
 
"Android" mobilių programėlių kūrimo įvadas #3
"Android" mobilių programėlių kūrimo įvadas #3"Android" mobilių programėlių kūrimo įvadas #3
"Android" mobilių programėlių kūrimo įvadas #3
 
MA DHARSH.pptx
MA DHARSH.pptxMA DHARSH.pptx
MA DHARSH.pptx
 
Android - Saving data
Android - Saving dataAndroid - Saving data
Android - Saving data
 
Third-party App Stores on Android.pptx.pdf
Third-party App Stores on Android.pptx.pdfThird-party App Stores on Android.pptx.pdf
Third-party App Stores on Android.pptx.pdf
 
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
 
Building Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsBuilding Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture Components
 
Storage 8
Storage   8Storage   8
Storage 8
 
Basic Android
Basic AndroidBasic Android
Basic Android
 
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdfPertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
 
Solid fire cloudstack storage overview - CloudStack European User Group
Solid fire cloudstack storage overview - CloudStack European User GroupSolid fire cloudstack storage overview - CloudStack European User Group
Solid fire cloudstack storage overview - CloudStack European User Group
 
CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire
 

Recently uploaded

Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 

Recently uploaded (20)

Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 

Everything about storage - DroidconMtl 2015

  • 1. Everything About Storage on Android   by Cindy Potvin (@CindyPtn) for DroidCon Montreal 2015 http://blog.cindypotvin.com
  • 2.
  • 3. Storage hardware ● Varies depending on the device :  Internal memory  SD card
  • 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
  • 9. 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>
  • 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.
  • 13. SQLite Database - Example
  • 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();
  • 19. Life Cycle - When to save
  • 20. Life Cycle - When to restore