SlideShare une entreprise Scribd logo
1  sur  29
BY: AKASH UGALE
Data Sores in Android
Operating System
Data Storage In Android
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.
Shared Preferences
Useful for storing and retrieving primitive
data in key value pairs.
Lightweight usage, such as saving application
settings.
Typical usage of SharedPreferences is for saving
application such as username and password,
auto login flag, remember-user flag etc.
Shared Preferences Contd…..
The shared preferences information is stored in
an XML file on the device
 Typically in/data/data/<Your Application’s package
name>/shared_prefs
SharedPreferences can be associated with the entire
application, or to a specific activity.
Use the getSharedPrefernces() method to get access
to the preferences
Shared Preferences Contd…..
Example:
 SharedPrefernces prefs = this.getSharedPrefernces(‘myPrefs,
MODE_PRIVATE’)
If the preferences XML file exist, it is opened,
otherwise it is created.
To Control access permission to the file:
 MODE_PRIVATE:private only to the application
 MODE_WORLD_READABLE:all application can read XML file
 MODE_WORLD_WRITABLE:all application can write XML file
Shared Preferences Contd…..
To add Shared preferences, first an editor object is
needed
 Editor prefsEditor = prefs.edit();
Then,use the put() method to add the key-value pairs
 prefsEditor.putString(“username”,”D-Link”);
 prefsEditor.putString(“password”,”vlsi#1@2”);
 prefsEditor.putint(“times-login”,1);
 prefsEditor.commit();
Shared Preferences Contd…..
To retrieve shared preferences data:
 String username = prefsEditor.getString(“username”.” ”);
 String password = prefsEditor.getString(“password”.” ”);
If you are using SharedPrefernces for specific
activity, then use getPreferences() method
 No need to specify the name of the preferences file
Example
Internal Storage
Android can save files directly to the device
internal storage.
These files are private to the application and will
be removed if you uninstall the application.
We can create a file using openFileOutput() with
parameter as file name and the operating mode.
Generally not recommended to use files.
Internal Storage Contd….
Similarly, we can open the file using
openFileInput() passing the parameter as the
filename with extension.
File are use to store large amount of data
Use I/O interfaces provided by java.io.* libraries
to read/write files.
Only local files can be accessed.
File Operation(Read)
Use context.openFileInput(string name) to
open a private input file stream related to a program.
Throw FileNotFoundException when file does
not exist.
Syntax:-
fileinputStram.in=this.openfileinput(“xyz.txt”)
.
.
.
.
.
In.close()://Close input stream
File Operation (Write)
Use context.openFileOutput(string name,int
mode ) to open a private output file stream related
to a program.
The file will be created if it does not exist.
Output stream can be opened in append mode,
which means new data will be appended to end of
the file.
String mystring=“Hello World”
File Operation (write) Contd….
Syntax:-
FileOutputStream outfile = this.openFileOutput(“myfile.txt”,MODE_APPEND)
//Open and Write in”myfile.txt”,using append mode.
Outfile.write(mystring.getBytes());
Outfile.close();//close output stream
External Storage
Every Android-compatible device supports a shared
“external storage” that you can use to save files
 Removable storage media (such as an SD card)
 Internal (non-removable) storage
File saved to the external storage are world readable
and can be modified by the user when they enable
USB mass storage to transfer files on computer.
These files are private to the application and will
be removed when the application is uninstalled.
External Storage Continue
Must check whether external storage is available first
by calling getExternalStorageState()
 Also check whether it allows read/write before reading/writing
on it
getExternalFilesDir() takes a parameter such as
DIRECTORY_MUSIC,DIRECTORY_RINGTONE
etc,to open specific type of subdirectories.
For public shared directories,use
getExternalStoragePublicDirectory()
External Storage Contd…..
For cache files,use getExternalCacheDir()
All these are applicable for API level 8 or above
For API level 7 or below ,use the method;
 getExternalStorageDirectory()
 Private files stored in//Android/data/<package_name>/files/
 Cache files stored in//Android/data/<package_name>/cache/
Example
SQLite Databases
android.database.sqlite Contains the SQLite
database management classes that an application
would use to manage its own private database.
android.database.sqlite.SQLiteDatabase
Contains the methods for: creating, opening, closing,
inserting, updating, deleting and quering an SQLite
database.
android.database.sqlite - Classes
 SQLiteCloseable - An object created from a SQLiteDatabase that can be
closed.
 SQLiteCursor - A Cursor implementation that exposes results from a
query on a SQLiteDatabase.
 SQLiteDatabase - Exposes methods to manage a SQLite database.
 SQLiteOpenHelper - A helper class to manage database creation and
version management.
 SQLiteProgram - A base class for compiled SQLite programs.
 SQLiteQuery - A SQLite program that represents a query that reads the
resulting rows into a CursorWindow.
 SQLiteQueryBuilder - a convenience class that helps build SQL queries
to be sent to SQLiteDatabase objects.
 SQLiteStatement - A pre-compiled statement against a SQLiteDatabase
that can be reused.
OpenOrCreateDatabase
This method will open an existing database or create
one in the application data area
 import android.database.sqlite.SQLiteDatabase;
SQLiteDatabase myDatabase;
myDatabase = openOrCreateDatabase ("my_sqlite_database.db" ,
SQLiteDatabase.CREATE_IF_NECESSARY , null);
Creating Tables
Create a static string containing the SQLite
CREATE statement, use the execSQL( ) method to
execute it.
 String createAuthor = "CREAT TABLE authors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fname TEXT,
lname TEXT);
myDatabase.execSQL(createAuthor);
insert( ), delete( )
long insert(String table, String nullColumnHack,
ContentValues values)
import android.content.ContentValues;
ContentValues values = new ContentValues( );
values.put("firstname" , "J.K.");
values.put("lastname" , "Rowling");
long newAuthorID = myDatabase.insert("tbl_authors" , "" ,
values);
int delete(String table, String whereClause, String[]
whereArgs)
public void deleteBook(Integer bookId) {
myDatabase.delete("tbl_books" , "id=?" ,
new String[ ] { bookId.toString( ) } ) ;
}
Update()
int update(String table, ContentValues values, String
whereClause, String[ ] whereArgs)
public void updateBookTitle(Integer bookId, String newTitle) {
ContentValues values = new ContentValues();
values.put("title" , newTitle);
myDatabase.update("tbl_books" , values ,
"id=?" , new String[ ] {bookId.toString() } );
}
SQLite Storage
Content Provider
Cloud Storage
 Online file storage
centres or cloud
storage providers
allow you to safely
upload your files
to the Internet.
Cloud Storage Contd…..
There are various providers of cloud storage
Examples:
Apple iCloud(Gives 5GB of free storage )
Dropbox(Gives 2GB of free storage )
Google Drive(Gives 15GB of free storage )
Amazon Cloud Drive(Gives 5GB of free storage
Microsoft SkyDrive(Gives 7GB of free storage )
Example
Data Storage In Android

Contenu connexe

Tendances (20)

Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Notification android
Notification androidNotification android
Notification android
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Fragment
Fragment Fragment
Fragment
 
Android intents
Android intentsAndroid intents
Android intents
 
Android Toast.pdf
Android Toast.pdfAndroid Toast.pdf
Android Toast.pdf
 
Java swing
Java swingJava swing
Java swing
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Database in Android
Database in AndroidDatabase in Android
Database in Android
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
android content providers
android content providersandroid content providers
android content providers
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Android animation
Android animationAndroid animation
Android animation
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 

Similaire à Data Storage In Android

Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Khaled Anaqwa
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptxFaezNasir
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 StorageDiego Grancini
 
Assets, files, and data parsing
Assets, files, and data parsingAssets, files, and data parsing
Assets, files, and data parsingAly Arman
 
03 programmation mobile - android - (stockage, multithreads, web services)
03 programmation mobile - android - (stockage, multithreads, web services)03 programmation mobile - android - (stockage, multithreads, web services)
03 programmation mobile - android - (stockage, multithreads, web services)TECOS
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behesteeHussain Behestee
 
Mobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdfMobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdfAbdullahMunir32
 
Persistence on iOS
Persistence on iOSPersistence on iOS
Persistence on iOSMake School
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardJAX London
 
Android App Development 05 : Saving Data
Android App Development 05 : Saving DataAndroid App Development 05 : Saving Data
Android App Development 05 : Saving DataAnuchit Chalothorn
 
Tk2323 lecture 7 data storage
Tk2323 lecture 7   data storageTk2323 lecture 7   data storage
Tk2323 lecture 7 data storageMengChun Lam
 
Everything about storage - DroidconMtl 2015
Everything about storage - DroidconMtl 2015Everything about storage - DroidconMtl 2015
Everything about storage - DroidconMtl 2015Cindy Potvin
 
09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WPNguyen Tuan
 
"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
 

Similaire à Data Storage In Android (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 Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)
 
Android - Saving data
Android - Saving dataAndroid - Saving data
Android - Saving data
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx
 
Android App Development - 09 Storage
Android App Development - 09 StorageAndroid App Development - 09 Storage
Android App Development - 09 Storage
 
Data management
Data managementData management
Data management
 
Data management
Data managementData management
Data management
 
Assets, files, and data parsing
Assets, files, and data parsingAssets, files, and data parsing
Assets, files, and data parsing
 
03 programmation mobile - android - (stockage, multithreads, web services)
03 programmation mobile - android - (stockage, multithreads, web services)03 programmation mobile - android - (stockage, multithreads, web services)
03 programmation mobile - android - (stockage, multithreads, web services)
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behestee
 
Mobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdfMobile Application Development-Lecture 13 & 14.pdf
Mobile Application Development-Lecture 13 & 14.pdf
 
Persistence on iOS
Persistence on iOSPersistence on iOS
Persistence on iOS
 
Storage 8
Storage   8Storage   8
Storage 8
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
 
Memory management
Memory managementMemory management
Memory management
 
Android App Development 05 : Saving Data
Android App Development 05 : Saving DataAndroid App Development 05 : Saving Data
Android App Development 05 : Saving Data
 
Tk2323 lecture 7 data storage
Tk2323 lecture 7   data storageTk2323 lecture 7   data storage
Tk2323 lecture 7 data storage
 
Everything about storage - DroidconMtl 2015
Everything about storage - DroidconMtl 2015Everything about storage - DroidconMtl 2015
Everything about storage - DroidconMtl 2015
 
09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP09.Local Database Files and Storage on WP
09.Local Database Files and Storage on WP
 
"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
 

Dernier

Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEselvakumar948
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 

Dernier (20)

Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 

Data Storage In Android

  • 1. BY: AKASH UGALE Data Sores in Android Operating System
  • 2. Data Storage In Android 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. Shared Preferences Useful for storing and retrieving primitive data in key value pairs. Lightweight usage, such as saving application settings. Typical usage of SharedPreferences is for saving application such as username and password, auto login flag, remember-user flag etc.
  • 4. Shared Preferences Contd….. The shared preferences information is stored in an XML file on the device  Typically in/data/data/<Your Application’s package name>/shared_prefs SharedPreferences can be associated with the entire application, or to a specific activity. Use the getSharedPrefernces() method to get access to the preferences
  • 5. Shared Preferences Contd….. Example:  SharedPrefernces prefs = this.getSharedPrefernces(‘myPrefs, MODE_PRIVATE’) If the preferences XML file exist, it is opened, otherwise it is created. To Control access permission to the file:  MODE_PRIVATE:private only to the application  MODE_WORLD_READABLE:all application can read XML file  MODE_WORLD_WRITABLE:all application can write XML file
  • 6. Shared Preferences Contd….. To add Shared preferences, first an editor object is needed  Editor prefsEditor = prefs.edit(); Then,use the put() method to add the key-value pairs  prefsEditor.putString(“username”,”D-Link”);  prefsEditor.putString(“password”,”vlsi#1@2”);  prefsEditor.putint(“times-login”,1);  prefsEditor.commit();
  • 7. Shared Preferences Contd….. To retrieve shared preferences data:  String username = prefsEditor.getString(“username”.” ”);  String password = prefsEditor.getString(“password”.” ”); If you are using SharedPrefernces for specific activity, then use getPreferences() method  No need to specify the name of the preferences file
  • 9. Internal Storage Android can save files directly to the device internal storage. These files are private to the application and will be removed if you uninstall the application. We can create a file using openFileOutput() with parameter as file name and the operating mode. Generally not recommended to use files.
  • 10. Internal Storage Contd…. Similarly, we can open the file using openFileInput() passing the parameter as the filename with extension. File are use to store large amount of data Use I/O interfaces provided by java.io.* libraries to read/write files. Only local files can be accessed.
  • 11. File Operation(Read) Use context.openFileInput(string name) to open a private input file stream related to a program. Throw FileNotFoundException when file does not exist. Syntax:- fileinputStram.in=this.openfileinput(“xyz.txt”) . . . . . In.close()://Close input stream
  • 12. File Operation (Write) Use context.openFileOutput(string name,int mode ) to open a private output file stream related to a program. The file will be created if it does not exist. Output stream can be opened in append mode, which means new data will be appended to end of the file. String mystring=“Hello World”
  • 13. File Operation (write) Contd…. Syntax:- FileOutputStream outfile = this.openFileOutput(“myfile.txt”,MODE_APPEND) //Open and Write in”myfile.txt”,using append mode. Outfile.write(mystring.getBytes()); Outfile.close();//close output stream
  • 14. External Storage Every Android-compatible device supports a shared “external storage” that you can use to save files  Removable storage media (such as an SD card)  Internal (non-removable) storage File saved to the external storage are world readable and can be modified by the user when they enable USB mass storage to transfer files on computer. These files are private to the application and will be removed when the application is uninstalled.
  • 15. External Storage Continue Must check whether external storage is available first by calling getExternalStorageState()  Also check whether it allows read/write before reading/writing on it getExternalFilesDir() takes a parameter such as DIRECTORY_MUSIC,DIRECTORY_RINGTONE etc,to open specific type of subdirectories. For public shared directories,use getExternalStoragePublicDirectory()
  • 16. External Storage Contd….. For cache files,use getExternalCacheDir() All these are applicable for API level 8 or above For API level 7 or below ,use the method;  getExternalStorageDirectory()  Private files stored in//Android/data/<package_name>/files/  Cache files stored in//Android/data/<package_name>/cache/
  • 18. SQLite Databases android.database.sqlite Contains the SQLite database management classes that an application would use to manage its own private database. android.database.sqlite.SQLiteDatabase Contains the methods for: creating, opening, closing, inserting, updating, deleting and quering an SQLite database.
  • 19. android.database.sqlite - Classes  SQLiteCloseable - An object created from a SQLiteDatabase that can be closed.  SQLiteCursor - A Cursor implementation that exposes results from a query on a SQLiteDatabase.  SQLiteDatabase - Exposes methods to manage a SQLite database.  SQLiteOpenHelper - A helper class to manage database creation and version management.  SQLiteProgram - A base class for compiled SQLite programs.  SQLiteQuery - A SQLite program that represents a query that reads the resulting rows into a CursorWindow.  SQLiteQueryBuilder - a convenience class that helps build SQL queries to be sent to SQLiteDatabase objects.  SQLiteStatement - A pre-compiled statement against a SQLiteDatabase that can be reused.
  • 20. OpenOrCreateDatabase This method will open an existing database or create one in the application data area  import android.database.sqlite.SQLiteDatabase; SQLiteDatabase myDatabase; myDatabase = openOrCreateDatabase ("my_sqlite_database.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null);
  • 21. Creating Tables Create a static string containing the SQLite CREATE statement, use the execSQL( ) method to execute it.  String createAuthor = "CREAT TABLE authors ( id INTEGER PRIMARY KEY AUTOINCREMENT, fname TEXT, lname TEXT); myDatabase.execSQL(createAuthor);
  • 22. insert( ), delete( ) long insert(String table, String nullColumnHack, ContentValues values) import android.content.ContentValues; ContentValues values = new ContentValues( ); values.put("firstname" , "J.K."); values.put("lastname" , "Rowling"); long newAuthorID = myDatabase.insert("tbl_authors" , "" , values); int delete(String table, String whereClause, String[] whereArgs) public void deleteBook(Integer bookId) { myDatabase.delete("tbl_books" , "id=?" , new String[ ] { bookId.toString( ) } ) ; }
  • 23. Update() int update(String table, ContentValues values, String whereClause, String[ ] whereArgs) public void updateBookTitle(Integer bookId, String newTitle) { ContentValues values = new ContentValues(); values.put("title" , newTitle); myDatabase.update("tbl_books" , values , "id=?" , new String[ ] {bookId.toString() } ); }
  • 26. Cloud Storage  Online file storage centres or cloud storage providers allow you to safely upload your files to the Internet.
  • 27. Cloud Storage Contd….. There are various providers of cloud storage Examples: Apple iCloud(Gives 5GB of free storage ) Dropbox(Gives 2GB of free storage ) Google Drive(Gives 15GB of free storage ) Amazon Cloud Drive(Gives 5GB of free storage Microsoft SkyDrive(Gives 7GB of free storage )