SlideShare a Scribd company logo
ANDROID TRAINING
SESSION – 4
-Hussain KMR Behestee
AGENDAS
• Shared Preferences
• Storages
• SQLite Database
– SQLite Open Helper
• Content Providers
– SQLite Programming
• Content Providers
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 save any primitive data: booleans, floats, ints, longs, and strings.
This data will persist across user sessions (even if your application is
killed).
• use one of two methods:
– getSharedPreferences() - Use this if you need multiple preferences files
identified by name.
– getPreferences() - Use this if you need only one preferences.
• 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 - getBoolean() and getString()
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
– 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()
• To read a file
– Call openFileInput() and pass it the name of the file
– Read bytes from the file with read()
– Then close the stream with close()
• Other useful methods: getFilesDir(), getDir(), deleteFile(), fileList()
External Storage
– Checking media availability - Before you do any work with the
external storage, you should always call getExternalStorageState() to
check whether the media is available.
– Accessing files on external storage
• If you're using API Level 8 or greater, use getExternalFilesDir() to open a File
• If you're using API Level 7 or lower, use getExternalStorageDirectory()
– You should then write your data in the following directory:
• /Android/data/<package_name>/files/
• If the user's device is running API Level 8 or greater and they uninstall your
application, this directory and all its contents will be deleted.
– Hiding your files from the Media Scanner
• Include an empty file named .nomedia in your external files directory
External Storage
– Saving files that should be shared - These directories lay at the root of
the external storage
• Music/ - Media scanner classifies all media found here as user music.
• Podcasts/ - Media scanner classifies all media found here as a podcast.
• Ringtones/ - Media scanner classifies all media found here as a ringtone.
• Alarms/ - Media scanner classifies all media found here as an alarm sound.
• Notifications/ - Media scanner classifies all media found here as a notification
sound.
• Pictures/ - All photos (excluding those taken with the camera).
• Movies/ - All movies (excluding those taken with the camcorder).
• Download/ - Miscellaneous downloads.
– Getting Shared Files
• In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the
type of public directory you want, such as DIRECTORY_MUSIC,
• If you're using API Level 7 or lower, use getExternalStorageDirectory()
SQLite Database
– Android provides full support for SQLite databases. Any databases you
create will be accessible by name to any class in the application, but
not outside the application.
– to create a new SQLite database is to create a subclass of
SQLiteOpenHelper and override the onCreate() method. You should
use it to Create Table
– To Upgrade your database use onUpgrade() method. You should use it
to Alter Table
Android Content Provider
App 1
(Dialer)
App 2
(Messaging)
App 3
(Custom)
App 4
(Custom)
Content Provider 1
Data can be shared over different applications
Content Provider Basics
1. There are no common storage area that all Android application can access.
2. The only way: to share data across applications: Content Provider
3. Content providers store and retrieve data and make it accessible to all
applications.
Content Provider 2
Android Content Provider
Content Provider Basics (Contd.)
Android ships with a number of content providers for common data types:
1. Audio
2. Video
3. Images
4. Personal contact information etc
Content Provider provides the way to share the data between multiple applications.
For example, contact data is used by multiple applications (Dialer, Messaging etc.)
and must be stored in Content Provider to have common access.
A content provider is a class that implements a standard set of methods to let other
applications store and retrieve the type of data that is handled by that content provider.
Content Provider
data
App 1
App 2
Android Content Provider
Querying Data
Content Provide: URI
1. Each content provider exposes a public URI that uniquely identifies its data set.
2. A content provider that controls multiple data sets (multiple tables) exposes a
separate URI for each one.
3. All URIs for providers begin with the string "content://".
The content: scheme identifies the data as being controlled by a content provider.
URI samples:
<standard_prefix>://<authority>/<data_path>/<id>
For example, to retrieve all the bookmarks stored by our web browsers (in Android):
content://browser/bookmarks
Similarly, to retrieve all the contacts stored by the Contacts application:
content://contacts/people
To retrieve a particular contact, you can specify the URI with a specific ID:
content://contacts/people/3
Android Content Provider
Content Provide: URI
So we need three pieces of information to query a content provider:
1. The URI that identifies the provider
2. The names of the data fields you want to receive
3. The data types for those fields
If we are querying a particular record, you also need the ID for that record.
Some more examples:
content://media/internal/images URI return the list of all internal images on the device.
content://contacts/people/ URI return the list of all contact names on the device.
content://contacts/people/45 URI return the single result row, the contact with ID=45.
QUESTION?
THANK YOU

More Related Content

DOCX
Android-data storage in android-chapter21
PPTX
Chapter 10.1
PPTX
Android Insights - 3 [Content Providers]
PDF
File structure
PPTX
Storage 8
PDF
Data management
PDF
Data management
PDF
File system in operating system e learning
Android-data storage in android-chapter21
Chapter 10.1
Android Insights - 3 [Content Providers]
File structure
Storage 8
Data management
Data management
File system in operating system e learning

What's hot (14)

PPT
"Android" mobilių programėlių kūrimo įvadas #3
PPT
android content providers
PPTX
05 content providers - Android
DOC
CSCI6505 Project:Construct search engine using ML approach
PPTX
Android Training (Content Provider)
PPTX
Directory structure
PPTX
Assets, files, and data parsing
PPT
SQLITE Android
PPT
Chapter 6 Java IO File
PDF
SQLite Database Tutorial In Android
PPSX
Unit 4 File and Data Management
PPSX
Unit 4 file and data management
PPTX
Python Tutorial-Mining imgur images
PDF
Lab4 - android
"Android" mobilių programėlių kūrimo įvadas #3
android content providers
05 content providers - Android
CSCI6505 Project:Construct search engine using ML approach
Android Training (Content Provider)
Directory structure
Assets, files, and data parsing
SQLITE Android
Chapter 6 Java IO File
SQLite Database Tutorial In Android
Unit 4 File and Data Management
Unit 4 file and data management
Python Tutorial-Mining imgur images
Lab4 - android
Ad

Viewers also liked (20)

PPTX
Using sqlite database in android with sqlite manager browser add ons
PPTX
PPTX
Rajab Davudov - Android Database
PPTX
Databases in Android Application
PDF
Video Streaming: from the native Android player to unconventional devices
PPTX
Database in Android
DOCX
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
PDF
[Ultracode Munich #4] Short introduction to the new Android build system incl...
PPTX
Android Database Tutorial
PPTX
Data Handning with Sqlite for Android
PDF
Xamarin Traditional Approach & Xamarin.Forms
PPTX
Android AsyncTask Tutorial
PPTX
Introduction to Xamarin and Xamarin Forms
PPTX
09.1. Android - Local Database (Sqlite)
PDF
Android studio 2.0
PDF
Introduction to Android Studio
PDF
Android Protips: Advanced Topics for Expert Android App Developers
PDF
Android Development: Build Android App from Scratch
PPT
Bus Tracking Application in Android
Using sqlite database in android with sqlite manager browser add ons
Rajab Davudov - Android Database
Databases in Android Application
Video Streaming: from the native Android player to unconventional devices
Database in Android
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
[Ultracode Munich #4] Short introduction to the new Android build system incl...
Android Database Tutorial
Data Handning with Sqlite for Android
Xamarin Traditional Approach & Xamarin.Forms
Android AsyncTask Tutorial
Introduction to Xamarin and Xamarin Forms
09.1. Android - Local Database (Sqlite)
Android studio 2.0
Introduction to Android Studio
Android Protips: Advanced Topics for Expert Android App Developers
Android Development: Build Android App from Scratch
Bus Tracking Application in Android
Ad

Similar to Android session 4-behestee (20)

PDF
Android datastorage
PPTX
Mobile application Development-UNIT-V (1).pptx
PPTX
6CS027 Lecture 5 Files and Database Storage (1).pptx
PDF
Mobile Application Development-Lecture 13 & 14.pdf
PPT
Memory management
PPT
Data Storage In Android
PDF
Mobile Application Development -Lecture 11 & 12.pdf
PPTX
12_Data_Storage_Part_2.pptx
PPT
iOS Application Pentesting
PPTX
MA DHARSH.pptx
PPTX
Share preference
PDF
Android local databases
PPT
Level 4
PPTX
Computer Software | Lecture 4D
PPTX
Computer Software - Lecture D
PPTX
Android application development fundamentals
PPT
iOS Application Penetration Testing for Beginners
PPTX
How to create android applications
PPTX
Analytics with unified file and object
PPTX
Internal Storage in Mobile Application Development.pptx
Android datastorage
Mobile application Development-UNIT-V (1).pptx
6CS027 Lecture 5 Files and Database Storage (1).pptx
Mobile Application Development-Lecture 13 & 14.pdf
Memory management
Data Storage In Android
Mobile Application Development -Lecture 11 & 12.pdf
12_Data_Storage_Part_2.pptx
iOS Application Pentesting
MA DHARSH.pptx
Share preference
Android local databases
Level 4
Computer Software | Lecture 4D
Computer Software - Lecture D
Android application development fundamentals
iOS Application Penetration Testing for Beginners
How to create android applications
Analytics with unified file and object
Internal Storage in Mobile Application Development.pptx

More from Hussain Behestee (7)

PPTX
Android session-1-sajib
PPTX
Android session-5-sajib
PPTX
Android session 3-behestee
PPTX
Android session 2-behestee
PPT
iOS Training Session-3
PPTX
iOS Session-2
PPTX
iOS app dev Training - Session1
Android session-1-sajib
Android session-5-sajib
Android session 3-behestee
Android session 2-behestee
iOS Training Session-3
iOS Session-2
iOS app dev Training - Session1

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PDF
Pre independence Education in Inndia.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Computing-Curriculum for Schools in Ghana
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Computing-Curriculum for Schools in Ghana
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Android session 4-behestee

  • 1. ANDROID TRAINING SESSION – 4 -Hussain KMR Behestee
  • 2. AGENDAS • Shared Preferences • Storages • SQLite Database – SQLite Open Helper • Content Providers – SQLite Programming • Content Providers
  • 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 save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed). • use one of two methods: – getSharedPreferences() - Use this if you need multiple preferences files identified by name. – getPreferences() - Use this if you need only one preferences. • 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 - getBoolean() and getString()
  • 4. 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 – 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() • To read a file – Call openFileInput() and pass it the name of the file – Read bytes from the file with read() – Then close the stream with close() • Other useful methods: getFilesDir(), getDir(), deleteFile(), fileList()
  • 5. External Storage – Checking media availability - Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. – Accessing files on external storage • If you're using API Level 8 or greater, use getExternalFilesDir() to open a File • If you're using API Level 7 or lower, use getExternalStorageDirectory() – You should then write your data in the following directory: • /Android/data/<package_name>/files/ • If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted. – Hiding your files from the Media Scanner • Include an empty file named .nomedia in your external files directory
  • 6. External Storage – Saving files that should be shared - These directories lay at the root of the external storage • Music/ - Media scanner classifies all media found here as user music. • Podcasts/ - Media scanner classifies all media found here as a podcast. • Ringtones/ - Media scanner classifies all media found here as a ringtone. • Alarms/ - Media scanner classifies all media found here as an alarm sound. • Notifications/ - Media scanner classifies all media found here as a notification sound. • Pictures/ - All photos (excluding those taken with the camera). • Movies/ - All movies (excluding those taken with the camcorder). • Download/ - Miscellaneous downloads. – Getting Shared Files • In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, • If you're using API Level 7 or lower, use getExternalStorageDirectory()
  • 7. SQLite Database – Android provides full support for SQLite databases. Any databases you create will be accessible by name to any class in the application, but not outside the application. – to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method. You should use it to Create Table – To Upgrade your database use onUpgrade() method. You should use it to Alter Table
  • 8. Android Content Provider App 1 (Dialer) App 2 (Messaging) App 3 (Custom) App 4 (Custom) Content Provider 1 Data can be shared over different applications Content Provider Basics 1. There are no common storage area that all Android application can access. 2. The only way: to share data across applications: Content Provider 3. Content providers store and retrieve data and make it accessible to all applications. Content Provider 2
  • 9. Android Content Provider Content Provider Basics (Contd.) Android ships with a number of content providers for common data types: 1. Audio 2. Video 3. Images 4. Personal contact information etc Content Provider provides the way to share the data between multiple applications. For example, contact data is used by multiple applications (Dialer, Messaging etc.) and must be stored in Content Provider to have common access. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider. Content Provider data App 1 App 2
  • 10. Android Content Provider Querying Data Content Provide: URI 1. Each content provider exposes a public URI that uniquely identifies its data set. 2. A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one. 3. All URIs for providers begin with the string "content://". The content: scheme identifies the data as being controlled by a content provider. URI samples: <standard_prefix>://<authority>/<data_path>/<id> For example, to retrieve all the bookmarks stored by our web browsers (in Android): content://browser/bookmarks Similarly, to retrieve all the contacts stored by the Contacts application: content://contacts/people To retrieve a particular contact, you can specify the URI with a specific ID: content://contacts/people/3
  • 11. Android Content Provider Content Provide: URI So we need three pieces of information to query a content provider: 1. The URI that identifies the provider 2. The names of the data fields you want to receive 3. The data types for those fields If we are querying a particular record, you also need the ID for that record. Some more examples: content://media/internal/images URI return the list of all internal images on the device. content://contacts/people/ URI return the list of all contact names on the device. content://contacts/people/45 URI return the single result row, the contact with ID=45.