SlideShare a Scribd company logo
Android Data
Storage
Android Training
By Khaled Anaqwa
Your data storage options are
the following:
 Shared Preferences
 Store private primitive data in key-value pairs.
 Internal Storage
 Store private data on the device memory.
 External Storage
 Store public data on the shared external storage.
 SQLite Databases
 Store structured data in a private database.
 Network Connection
 Store data on the web with your own network server.
SharedPreferences
 The SharedPreferences class provides a
general framework that allows you to
save and retrieve persistent key-value
pairs of primitive data types.
 This data will persist across user sessions
(even if your application is killed).
To work with shared
preferences you need to
 Initialization
 Storing Data
 Commit changes
 Retrieving Data
 Clearing / Deleting Data
Initialization
Application shared preferences can be fetched
using getSharedPreferences() method.
You also need an editor to edit and save the
changes in shared preferences.
SharedPreferences pref =
getApplicationContext().getSharedPreferences(”FileName", 0); // 0 - for
private mode
Editor editor = pref.edit();
Application shared preferences
can be fetched using
 getSharedPreferences() - Use this if you
need multiple preferences files identified
by name, which you specify with the first
parameter.
 getPreferences() - Use this if you need
only one preferences file for your Activity.
Because this will be the only preferences
file for your Activity, you don't supply a
name.
Storing Data
You can save data into shared preferences using editor. All the
primitive data types like booleans, floats, ints, longs, and strings
are supported.
You need to put data as Key with Value.
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
Commit changes
Call editor.commit() in order to save changes to shared
preferences..
like
editor.commit();
Retrieving Data
Data can be retrived from saved preferences by calling
getString() (For string) method.
Remember this method should be called on Shared Preferences
not on Editor.
// returns stored preference value
// If value is not present return default value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Clearing / Deleting Data
If you want to delete from shared preferences you can call
remove(“key_name”) to delete that particular value.
If you want to delete all the data, call clear().
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes;
editor.clear(); //clear all the data from shared preferences
editor.commit(); // commit changes
Task
 Create Settings with ShaerdPresferance
 General Settings
 Language selection
 Text Size
 Email
 Textcolor
 System Settings
 Enable Notifications
 use Wifi
 About
Using the Internal Storage
 You can save files directly on the device's
internal storage. By default, files saved to
the internal storage are private to your
application and other applications
cannot access them.
 When the user uninstalls your application,
these files are removed.
To create and write a private
file to the internal storage:
 Call openFileOutput() with the name of
the file and the operating mode. This
returns a FileOutputStream.
 Write to the file with write().
 Close the stream with close().
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos =
openFileOutput(FILENAME,Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
To read a file from internal
storage:
 Call openFileInput() and pass it the name
of the file to read. This returns a
FileInputStream.
 Read bytes from the file with read().
 Then close the stream with close().
RAW Files
 If you want to save a static file in your
application at compile time, save the file
in your project res/raw/ directory.
 You can open it with
openRawResource(), passing the
R.raw.<filename> resource ID.
 This method returns an InputStream that
you can use to read the file (but you
cannot write to the original file).
Cache Files:
 If you'd like to cache some data, rather
than store it persistently, you should use
getCacheDir() to open a File that
represents the internal directory where
your application should save temporary
cache files.
PreferenceActivity
 This is the base class for an activity to
show a hierarchy of preferences to the
user.
 this functionality should now be found in
the new PreferenceFragment class.
 using PreferenceActivity in its old mode,
the documentation there applies to the
deprecated APIs here.
Android Training (Storing & Shared Preferences)

More Related Content

PPTX
Android Training (Storing data using SQLite)
PPTX
Android share preferences
PPT
Shared preferences
PPT
JDBC – Java Database Connectivity
PPTX
Android Data Storagefinal
PPT
SQLITE Android
PPTX
Share preference
PPT
JDBC Architecture and Drivers
Android Training (Storing data using SQLite)
Android share preferences
Shared preferences
JDBC – Java Database Connectivity
Android Data Storagefinal
SQLITE Android
Share preference
JDBC Architecture and Drivers

What's hot (20)

PPTX
JAVA AWT
PPT
Jdbc complete
PPT
Java Input Output and File Handling
PPTX
Presentation on Android application life cycle and saved instancestate
PPTX
C# Framework class library
PPTX
Android activity lifecycle
PPT
4. Classes and Methods
PPT
C++ oop
PDF
Java: The Complete Reference, Eleventh Edition
PPSX
JDBC: java DataBase connectivity
PPT
JAVA OOP
PDF
Android ui dialog
PPTX
Android User Interface
PPTX
Introduction to java
PPT
Java Networking
PPT
Resource Bundle
PPT
JDBC
PPTX
Exception Handling in C#
PDF
Introduction to java (revised)
PPT
Log4 J
JAVA AWT
Jdbc complete
Java Input Output and File Handling
Presentation on Android application life cycle and saved instancestate
C# Framework class library
Android activity lifecycle
4. Classes and Methods
C++ oop
Java: The Complete Reference, Eleventh Edition
JDBC: java DataBase connectivity
JAVA OOP
Android ui dialog
Android User Interface
Introduction to java
Java Networking
Resource Bundle
JDBC
Exception Handling in C#
Introduction to java (revised)
Log4 J
Ad

Viewers also liked (20)

PPTX
Android Training (Content Provider)
PPT
Day 4: Android: UI Widgets
PDF
Android Fragment Pattern: Communication
PPTX
Android 101 - Introduction to Android Development
PPTX
Spinners, Adapters & Fragment Communication
PPTX
iOS course day 1
PPTX
iOS Course day 2
PPT
Data Storage In Android
ODP
Android App Development - 06 Fragments
PPTX
INSTALACIÓN ANDROID STUDIO 2
PDF
Android UI Testing with uiautomator
PDF
Android complete basic Guide
PDF
Android - Working with Fragments
PPSX
Screen orientations in android
PDF
Mobile design matters - iOS and Android
PDF
Android Data Persistence
PPTX
Android Training (android fundamental)
PPTX
Android training (android style)
PPTX
Android Training (Broadcast Receiver)
PPTX
Android Training (Services)
Android Training (Content Provider)
Day 4: Android: UI Widgets
Android Fragment Pattern: Communication
Android 101 - Introduction to Android Development
Spinners, Adapters & Fragment Communication
iOS course day 1
iOS Course day 2
Data Storage In Android
Android App Development - 06 Fragments
INSTALACIÓN ANDROID STUDIO 2
Android UI Testing with uiautomator
Android complete basic Guide
Android - Working with Fragments
Screen orientations in android
Mobile design matters - iOS and Android
Android Data Persistence
Android Training (android fundamental)
Android training (android style)
Android Training (Broadcast Receiver)
Android Training (Services)
Ad

Similar to Android Training (Storing & Shared Preferences) (20)

PDF
Android - Saving data
DOCX
Android-data storage in android-chapter21
PPTX
Mobile Application Development (Shared Preferences) class-06
ODP
Android App Development - 09 Storage
PDF
Mobile Application Development-Lecture 13 & 14.pdf
PDF
Android datastorage
PPT
Level 4
PPTX
SHARED PREFERENCES IN ANDROID APPS.pptx
PPTX
SHARED PREFERENCES IN ANDROID APPS.pptx
PDF
Data management
PDF
Data management
PPT
Memory management
PPTX
Tk2323 lecture 7 data storage
PPTX
Mobile application Development-UNIT-V (1).pptx
PDF
Android Workshop 2013
PDF
09.Local Database Files and Storage on WP
PDF
03 programmation mobile - android - (stockage, multithreads, web services)
DOC
Data Storage
PPTX
WP7 HUB_Creando aplicaciones de Windows Phone
PPT
Slice for Distributed Persistence (JavaOne 2010)
Android - Saving data
Android-data storage in android-chapter21
Mobile Application Development (Shared Preferences) class-06
Android App Development - 09 Storage
Mobile Application Development-Lecture 13 & 14.pdf
Android datastorage
Level 4
SHARED PREFERENCES IN ANDROID APPS.pptx
SHARED PREFERENCES IN ANDROID APPS.pptx
Data management
Data management
Memory management
Tk2323 lecture 7 data storage
Mobile application Development-UNIT-V (1).pptx
Android Workshop 2013
09.Local Database Files and Storage on WP
03 programmation mobile - android - (stockage, multithreads, web services)
Data Storage
WP7 HUB_Creando aplicaciones de Windows Phone
Slice for Distributed Persistence (JavaOne 2010)

More from Khaled Anaqwa (10)

PPTX
Android Training (Notifications)
PPTX
Android Training (Animation)
PPTX
Android Training (Touch)
PPTX
Android Training (Sensors)
PPTX
Android Training (Media)
PPTX
Android Training (AdapterView & Adapter)
PPTX
Android Training (ScrollView , Horizontal ScrollView WebView)
PPTX
Android Training (Android UI)
PPTX
Android Training (Intro)
PPTX
Android Training (Java Review)
Android Training (Notifications)
Android Training (Animation)
Android Training (Touch)
Android Training (Sensors)
Android Training (Media)
Android Training (AdapterView & Adapter)
Android Training (ScrollView , Horizontal ScrollView WebView)
Android Training (Android UI)
Android Training (Intro)
Android Training (Java Review)

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Spectral efficient network and resource selection model in 5G networks
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
sap open course for s4hana steps from ECC to s4
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I

Android Training (Storing & Shared Preferences)

  • 2. Your data storage options are the following:  Shared Preferences  Store private primitive data in key-value pairs.  Internal Storage  Store private data on the device memory.  External Storage  Store public data on the shared external storage.  SQLite Databases  Store structured data in a private database.  Network Connection  Store data on the web with your own network server.
  • 3. SharedPreferences  The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.  This data will persist across user sessions (even if your application is killed).
  • 4. To work with shared preferences you need to  Initialization  Storing Data  Commit changes  Retrieving Data  Clearing / Deleting Data
  • 5. Initialization Application shared preferences can be fetched using getSharedPreferences() method. You also need an editor to edit and save the changes in shared preferences. SharedPreferences pref = getApplicationContext().getSharedPreferences(”FileName", 0); // 0 - for private mode Editor editor = pref.edit();
  • 6. Application shared preferences can be fetched using  getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.  getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
  • 7. Storing Data You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. You need to put data as Key with Value. editor.putBoolean("key_name", true); // Storing boolean - true/false editor.putString("key_name", "string value"); // Storing string editor.putInt("key_name", "int value"); // Storing integer editor.putFloat("key_name", "float value"); // Storing float editor.putLong("key_name", "long value"); // Storing long
  • 8. Commit changes Call editor.commit() in order to save changes to shared preferences.. like editor.commit();
  • 9. Retrieving Data Data can be retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor. // returns stored preference value // If value is not present return default value - In this case null pref.getString("key_name", null); // getting String pref.getInt("key_name", null); // getting Integer pref.getFloat("key_name", null); // getting Float pref.getLong("key_name", null); // getting Long pref.getBoolean("key_name", null); // getting boolean
  • 10. Clearing / Deleting Data If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear(). editor.remove("name"); // will delete key name editor.remove("email"); // will delete key email editor.commit(); // commit changes; editor.clear(); //clear all the data from shared preferences editor.commit(); // commit changes
  • 11. Task  Create Settings with ShaerdPresferance  General Settings  Language selection  Text Size  Email  Textcolor  System Settings  Enable Notifications  use Wifi  About
  • 12. Using the Internal Storage  You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them.  When the user uninstalls your application, these files are removed.
  • 13. To create and write a private file to the internal storage:  Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.  Write to the file with write().  Close the stream with close().
  • 14. String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();
  • 15. To read a file from internal storage:  Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.  Read bytes from the file with read().  Then close the stream with close().
  • 16. RAW Files  If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory.  You can open it with openRawResource(), passing the R.raw.<filename> resource ID.  This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
  • 17. Cache Files:  If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files.
  • 18. PreferenceActivity  This is the base class for an activity to show a hierarchy of preferences to the user.  this functionality should now be found in the new PreferenceFragment class.  using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here.