SlideShare a Scribd company logo
Android
By : Vikash Patel
Vicky_1886@yahoo.com
+91 - 9429350401
Content
• 1. Data Storage ?
– 1.1. Shared Preferences
– 1.2. Internal Storage
Shared Preferences
• The SharedPreferences class provides a general
framework that allows you to save and retrieve
persistent key-value pairs of primitive data
types.
• You can use SharedPreferences to save any
primitive data: booleans, floats, ints, longs, and
strings. This data will persist across user
sessions (even if your application is killed).
• To get a Shared Preferences object for your
application, use one of two methods: Back …
Continue …
• 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.
Back …
Continue …
• To write values:
• Call edit() to get a SharedPreferences.Editor.
• Add values with methods such
as putBoolean() and putString().
• Commit the new values with commit()
• To read values, use SharedPreferences methods
such as getBoolean() and getString(). Back …
Continue …
Back …
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 (nor can the user).
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().
Back …
Continue …
Back …
Continue …
• MODE_PRIVATE will create the file (or replace a file
of the same name) and make it private to your
application. Other modes available
are: MODE_APPEND, MODE_WORLD_READABLE,
and MODE_WORLD_WRITEABLE.
• 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().
Back …
Continue …
• Saving 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.
– When the device is low on internal storage space,
Android may delete these cache files to recover space.
However, you should not rely on the system to clean up
these files for you.
– You should always maintain the cache files yourself and
stay within a reasonable limit of space consumed, such
as 1MB. When the user uninstalls your application, these
files are removed. Back …

More Related Content

PPTX
Storage 8
PPTX
Chapter 10.3
PPTX
File Handling in Java Oop presentation
PPTX
PPTX
Is2215 lecture6 lecturer_file_access
PDF
Data management
PDF
Data management
PPSX
Storage 8
Chapter 10.3
File Handling in Java Oop presentation
Is2215 lecture6 lecturer_file_access
Data management
Data management

What's hot (20)

PDF
Basic i/o & file handling in java
PPT
Chapter 6 Java IO File
PPTX
C# File IO Operations
PPTX
Android Training (Java Review)
PPT
File handling
PPTX
Chapter 10.1
PPT
Input output streams
PPT
3 ref works 2.0 importing text files
 
PPTX
File handling
PPT
Level 4
PPT
Various io stream classes .47
PPTX
Java Input Output (java.io.*)
PPT
Level 1 & 2
PPT
Level 3
PPTX
Understanding java streams
PPTX
Windows Phone 8 - 11 App to App Communication
PDF
input/ output in java
ODP
Databases and doctrine
Basic i/o & file handling in java
Chapter 6 Java IO File
C# File IO Operations
Android Training (Java Review)
File handling
Chapter 10.1
Input output streams
3 ref works 2.0 importing text files
 
File handling
Level 4
Various io stream classes .47
Java Input Output (java.io.*)
Level 1 & 2
Level 3
Understanding java streams
Windows Phone 8 - 11 App to App Communication
input/ output in java
Databases and doctrine
Ad

Similar to Memory management (20)

PPTX
Android Training (Storing & Shared Preferences)
PDF
Android datastorage
DOCX
Android-data storage in android-chapter21
PPT
Data Storage In Android
PPTX
Android Data Storagefinal
PPTX
6CS027 Lecture 5 Files and Database Storage (1).pptx
PDF
03 programmation mobile - android - (stockage, multithreads, web services)
PDF
Android Data Persistence
PDF
Lab4 - android
PDF
Mobile Application Development-Lecture 13 & 14.pdf
PPTX
Tk2323 lecture 7 data storage
PPT
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
ODP
Android App Development - 09 Storage
PDF
Mobile Application Development -Lecture 11 & 12.pdf
PDF
Android App Development 05 : Saving Data
PDF
Android - Saving data
PPTX
MA DHARSH.pptx
PPTX
12_Data_Storage_Part_2.pptx
PDF
Everything about storage - DroidconMtl 2015
ODP
Android training day 4
Android Training (Storing & Shared Preferences)
Android datastorage
Android-data storage in android-chapter21
Data Storage In Android
Android Data Storagefinal
6CS027 Lecture 5 Files and Database Storage (1).pptx
03 programmation mobile - android - (stockage, multithreads, web services)
Android Data Persistence
Lab4 - android
Mobile Application Development-Lecture 13 & 14.pdf
Tk2323 lecture 7 data storage
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android App Development - 09 Storage
Mobile Application Development -Lecture 11 & 12.pdf
Android App Development 05 : Saving Data
Android - Saving data
MA DHARSH.pptx
12_Data_Storage_Part_2.pptx
Everything about storage - DroidconMtl 2015
Android training day 4
Ad

Memory management

  • 1. Android By : Vikash Patel Vicky_1886@yahoo.com +91 - 9429350401
  • 2. Content • 1. Data Storage ? – 1.1. Shared Preferences – 1.2. Internal Storage
  • 3. Shared Preferences • The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. • You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed). • To get a Shared Preferences object for your application, use one of two methods: Back …
  • 4. Continue … • 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. Back …
  • 5. Continue … • To write values: • Call edit() to get a SharedPreferences.Editor. • Add values with methods such as putBoolean() and putString(). • Commit the new values with commit() • To read values, use SharedPreferences methods such as getBoolean() and getString(). Back …
  • 7. 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 (nor can the user). 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(). Back …
  • 9. Continue … • MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. • 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(). Back …
  • 10. Continue … • Saving 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. – When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. – You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed. Back …