SlideShare a Scribd company logo
CS425 - Web and Mobile Software Engineering
Dr. Lilia Sfaxi
27 avril 2017
Storage
Android Lab 04


STORAGE DR. LILIA SFAXI !1
MedTech
I. Storage in Android
There are several options to store persistent data in Android. The choice of the ideal solution
depends on the specific needs of the users:
• Is the data private or accessible to other applications?
• How much space is required?
• Must the data be structures, semi-structured or not structured?
The storage options we present in this lab are the following:
1. Shared Preferences
It is a general framework that enables to save and extract key-value pairs of primitive type. It
enables to store and retrieve boolean, float, integer, long and string data.
Typically, the Shared Preferences are used to save the preferences of the users, such as the
ring.
2. Internal Storage
It is possible to use the internal storage of the device to save files. By default, those files are
private (inaccessible to other applications). When the application is deleted, these files are too.
3. External Storage
All Android compatible devices support external storage, that can be an SD card, or an
internal non-removable space.
Saved files in an external storage are accessible to all applications, in read-only mode. They
can be modified by the user if the « USB mass storage » is activated to transfer the files to a
computer.
4. SQLite Database
Android provides a total support for SQLite DBMS. SQLite is a software library
implementing a SQL database engine with zero-configuration, very light and without
external dependencies.
All databases created in an application are accessible by name from everywhere in this
application, but not from the outside of the application.
A recommended method to create a SQLite database is to use a sub-class of
SQLiteOpenHelper, a helper class that facilitates the creation and handling of SQLite
databases.
STORAGE DR. LILIA SFAXI !2
II. Exercise 1: Shared Preferences
1. Objective
The goal of this first part is to create a simple application to store some data in the Shared
Preferences. The graphical interface will be as follows:
2. Implementation
To read an object from Shared Preferences, use one of the following methods:
• getSharedPreferences: to use several preference files defined by name.
• getPreferences: to use a single preference file, without defining a file name.
To add values:
• Call edit() to get a SharedPreferences.Editor
• Add values with methods such as: putBoolean() and putString()
• Validate these values with commit()
To read values, use getBoolean(), getString(),…
The obtained code looks like the following:
STORAGE DR. LILIA SFAXI !3
Activity 1: Create a project called Lab4, enabling to save and
read data in the Shared Preferences.
III. Exercise 2: Internal and External Storage
1. Objective
The main objective of this part is to create a simple application to store some data in internal
and external files. The interface will look like the following:
STORAGE DR. LILIA SFAXI !4
1 2
2. Internal Storage
To read and write in a file internal to the application, use the Java primitives FileInputStream
and FileOutputStream. When writing data, it is mandatory to specify one (or many) operational
mode(s). These modes can be combined using | operator.
• MODE_PRIVATE : The file is accessible only for the application that created it.
• MODE_WORLD_READABLE : The file is accessible by other applications in read-
only.
• MODE_WORLD_WRITEABLE : The file is writable by other applications.
• MODE_APPEND : If the file already exists, the data is appended at the end.
Here are some examples to read and write internal files.
To display the files of your application, open the Android Device Monitor (under Tools ->
Android) and choose File Explorer . You can find the file you created under le directory /data/
1
data/<your project package>/files
There is a bug in the API 24 devices preventing the file explorer to be displayed. Use devices with lower API
1
for testing.
STORAGE DR. LILIA SFAXI !5
Activity 2:
-Create the interface in a new activity, use an intent to go
from the preceding activity (Shared Preferences) to this one.
-Test the behaviour of the methods readInternal and
saveInternal and display the result in a toast, for now.
3. External Storage
To read or write files on the external storage, the application must have
READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions.
Before manipulating the storage, il must first verify its availability, using the method
getExternalStorageState.
Example of writing:
STORAGE DR. LILIA SFAXI !6
Example of reading:
Activity 3: Add the part where you write in an external file.
Change the layout in order to display the read text in the
corresponding TextArea (as showed in the interface above).
STORAGE DR. LILIA SFAXI !7

More Related Content

PDF
Lab3-Android
PDF
Lab 5-Android
PDF
Angular
PDF
Client-side JavaScript
DOCX
Struts notes
PDF
Exploring Maven SVN GIT
PDF
Javascript Design Patterns
PDF
Angular Best Practices - Perfomatix
Lab3-Android
Lab 5-Android
Angular
Client-side JavaScript
Struts notes
Exploring Maven SVN GIT
Javascript Design Patterns
Angular Best Practices - Perfomatix

What's hot (20)

ODT
Spring IOC advantages and developing spring application sample
PPTX
ODT
Types of Dependency Injection in Spring
PPTX
java drag and drop and data transfer
DOCX
Learning MVC Part 3 Creating MVC Application with EntityFramework
PPTX
Pragmatic orchard
PPT
AspMVC4 start101
PDF
Ee java lab assignment 4
DOCX
02 java spring-hibernate-experience-questions
PPT
DJango
PPT
Java for Mainframers
ODP
Different Types of Containers in Spring
PDF
Leverage Hibernate and Spring Features Together
PPT
10 reasons to choose the yii framework
PDF
Introduction to Spring's Dependency Injection
DOCX
LearningMVCWithLINQToSQL
PPTX
Spring andspringboot training
PPTX
CodeIgniter 101 Tutorial
PDF
.NET Core, ASP.NET Core Course, Session 14
PPTX
Spring essentials 2 Spring Series 02)
Spring IOC advantages and developing spring application sample
Types of Dependency Injection in Spring
java drag and drop and data transfer
Learning MVC Part 3 Creating MVC Application with EntityFramework
Pragmatic orchard
AspMVC4 start101
Ee java lab assignment 4
02 java spring-hibernate-experience-questions
DJango
Java for Mainframers
Different Types of Containers in Spring
Leverage Hibernate and Spring Features Together
10 reasons to choose the yii framework
Introduction to Spring's Dependency Injection
LearningMVCWithLINQToSQL
Spring andspringboot training
CodeIgniter 101 Tutorial
.NET Core, ASP.NET Core Course, Session 14
Spring essentials 2 Spring Series 02)
Ad

Similar to Lab4 - android (20)

PPTX
Android session 4-behestee
PDF
Android datastorage
PPT
Memory management
PPTX
Storage 8
PPTX
6CS027 Lecture 5 Files and Database Storage (1).pptx
DOCX
Android-data storage in android-chapter21
PDF
Mobile Application Development -Lecture 11 & 12.pdf
PDF
Mobile Application Development-Lecture 13 & 14.pdf
PPTX
Internal Storage in Mobile Application Development.pptx
PDF
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
PPTX
MA DHARSH.pptx
PPTX
Share preference
PPT
Data Storage In Android
PPTX
12_Data_Storage_Part_2.pptx
PPT
iOS Application Pentesting
PPTX
How to create android applications
PDF
Scoped storage in android 10 all you need to know
PPT
Android project architecture
PDF
Introduction to Android Development and Security
PPT
iOS Application Penetration Testing for Beginners
Android session 4-behestee
Android datastorage
Memory management
Storage 8
6CS027 Lecture 5 Files and Database Storage (1).pptx
Android-data storage in android-chapter21
Mobile Application Development -Lecture 11 & 12.pdf
Mobile Application Development-Lecture 13 & 14.pdf
Internal Storage in Mobile Application Development.pptx
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
MA DHARSH.pptx
Share preference
Data Storage In Android
12_Data_Storage_Part_2.pptx
iOS Application Pentesting
How to create android applications
Scoped storage in android 10 all you need to know
Android project architecture
Introduction to Android Development and Security
iOS Application Penetration Testing for Beginners
Ad

More from Lilia Sfaxi (20)

PDF
chp1-Intro à l'urbanisation des SI.pdf
PDF
Plan d'études_INSAT_2022_2023.pdf
PDF
Lab3-DB_Neo4j
PDF
Lab2-DB-Mongodb
PDF
Lab1-DB-Cassandra
PDF
TP2-UML-Correction
PDF
TP1-UML-Correction
PDF
TP0-UML-Correction
PDF
TD4-UML
PDF
TD4-UML-Correction
PDF
TD3-UML-Séquences
PDF
TD3-UML-Correction
PDF
TD2 - UML - Correction
PDF
TD1 - UML - DCU
PDF
TD1-UML-correction
PDF
Android - Tp1 - installation et démarrage
PDF
Android - Tp2 - Elements graphiques
PDF
Android - Tp3 - intents
PDF
Android - TPBonus - web services
PDF
Android - Tp4 - graphiques avancés
chp1-Intro à l'urbanisation des SI.pdf
Plan d'études_INSAT_2022_2023.pdf
Lab3-DB_Neo4j
Lab2-DB-Mongodb
Lab1-DB-Cassandra
TP2-UML-Correction
TP1-UML-Correction
TP0-UML-Correction
TD4-UML
TD4-UML-Correction
TD3-UML-Séquences
TD3-UML-Correction
TD2 - UML - Correction
TD1 - UML - DCU
TD1-UML-correction
Android - Tp1 - installation et démarrage
Android - Tp2 - Elements graphiques
Android - Tp3 - intents
Android - TPBonus - web services
Android - Tp4 - graphiques avancés

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Cloud computing and distributed systems.
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
sap open course for s4hana steps from ECC to s4
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
Spectroscopy.pptx food analysis technology
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Encapsulation theory and applications.pdf
sap open course for s4hana steps from ECC to s4

Lab4 - android

  • 1. CS425 - Web and Mobile Software Engineering Dr. Lilia Sfaxi 27 avril 2017 Storage Android Lab 04 
 STORAGE DR. LILIA SFAXI !1 MedTech
  • 2. I. Storage in Android There are several options to store persistent data in Android. The choice of the ideal solution depends on the specific needs of the users: • Is the data private or accessible to other applications? • How much space is required? • Must the data be structures, semi-structured or not structured? The storage options we present in this lab are the following: 1. Shared Preferences It is a general framework that enables to save and extract key-value pairs of primitive type. It enables to store and retrieve boolean, float, integer, long and string data. Typically, the Shared Preferences are used to save the preferences of the users, such as the ring. 2. Internal Storage It is possible to use the internal storage of the device to save files. By default, those files are private (inaccessible to other applications). When the application is deleted, these files are too. 3. External Storage All Android compatible devices support external storage, that can be an SD card, or an internal non-removable space. Saved files in an external storage are accessible to all applications, in read-only mode. They can be modified by the user if the « USB mass storage » is activated to transfer the files to a computer. 4. SQLite Database Android provides a total support for SQLite DBMS. SQLite is a software library implementing a SQL database engine with zero-configuration, very light and without external dependencies. All databases created in an application are accessible by name from everywhere in this application, but not from the outside of the application. A recommended method to create a SQLite database is to use a sub-class of SQLiteOpenHelper, a helper class that facilitates the creation and handling of SQLite databases. STORAGE DR. LILIA SFAXI !2
  • 3. II. Exercise 1: Shared Preferences 1. Objective The goal of this first part is to create a simple application to store some data in the Shared Preferences. The graphical interface will be as follows: 2. Implementation To read an object from Shared Preferences, use one of the following methods: • getSharedPreferences: to use several preference files defined by name. • getPreferences: to use a single preference file, without defining a file name. To add values: • Call edit() to get a SharedPreferences.Editor • Add values with methods such as: putBoolean() and putString() • Validate these values with commit() To read values, use getBoolean(), getString(),… The obtained code looks like the following: STORAGE DR. LILIA SFAXI !3
  • 4. Activity 1: Create a project called Lab4, enabling to save and read data in the Shared Preferences. III. Exercise 2: Internal and External Storage 1. Objective The main objective of this part is to create a simple application to store some data in internal and external files. The interface will look like the following: STORAGE DR. LILIA SFAXI !4 1 2
  • 5. 2. Internal Storage To read and write in a file internal to the application, use the Java primitives FileInputStream and FileOutputStream. When writing data, it is mandatory to specify one (or many) operational mode(s). These modes can be combined using | operator. • MODE_PRIVATE : The file is accessible only for the application that created it. • MODE_WORLD_READABLE : The file is accessible by other applications in read- only. • MODE_WORLD_WRITEABLE : The file is writable by other applications. • MODE_APPEND : If the file already exists, the data is appended at the end. Here are some examples to read and write internal files. To display the files of your application, open the Android Device Monitor (under Tools -> Android) and choose File Explorer . You can find the file you created under le directory /data/ 1 data/<your project package>/files There is a bug in the API 24 devices preventing the file explorer to be displayed. Use devices with lower API 1 for testing. STORAGE DR. LILIA SFAXI !5
  • 6. Activity 2: -Create the interface in a new activity, use an intent to go from the preceding activity (Shared Preferences) to this one. -Test the behaviour of the methods readInternal and saveInternal and display the result in a toast, for now. 3. External Storage To read or write files on the external storage, the application must have READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. Before manipulating the storage, il must first verify its availability, using the method getExternalStorageState. Example of writing: STORAGE DR. LILIA SFAXI !6
  • 7. Example of reading: Activity 3: Add the part where you write in an external file. Change the layout in order to display the read text in the corresponding TextArea (as showed in the interface above). STORAGE DR. LILIA SFAXI !7