0% found this document useful (0 votes)
4 views10 pages

7-Android Storage 755a

The document provides an overview of data storage options in Android, including SharedPreferences for key-value pairs, internal and external file storage, and databases using the Room persistence library. It outlines how to write and read data using SharedPreferences and access files in internal and external storage. Additionally, it mentions advanced options like EncryptedSharedPreferences and DataStore for storing typed objects.

Uploaded by

ffghostdz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

7-Android Storage 755a

The document provides an overview of data storage options in Android, including SharedPreferences for key-value pairs, internal and external file storage, and databases using the Room persistence library. It outlines how to write and read data using SharedPreferences and access files in internal and external storage. Additionally, it mentions advanced options like EncryptedSharedPreferences and DataStore for storing typed objects.

Uploaded by

ffghostdz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Storage in Android

Data Storage in Android

Mohammed Said BELAID


[email protected]

25/04/2024
@USTO → FMI → Dept Info → L3 SI
Data Storage in Android

Outline

1 Intoduction

2 SharedPreferences

3 Files

4 Database
Data Storage in Android
Intoduction

Types

SharedPreferences: Store private, primitive data in


key-value pairs.
App-specic storage (Internal storage): Store les that are
meant for your app's use only.
Shared storage (External storage): Store les that your app
intends to share with other apps
Databases: Store structured data in a private database using
the Room persistence library.
Data Storage in Android
SharedPreferences

SharedPreferences

store a small collection of values


use key value pairs
only primitive types
Data Storage in Android
SharedPreferences

Write SharedPreferences

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)


?: return
with (sharedPref.edit()) {
putInt(getString(R.string.saved_high_score_key), newHighScore
)
apply()
}
Data Storage in Android
SharedPreferences

Read SharedPreferences

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)


?: return
val defaultValue = resources.getInteger(R.integer.
saved_high_score_default_key)
val highScore = sharedPref.getInt(getString(R.string.
saved_high_score_key), defaultValue)
Data Storage in Android
SharedPreferences

Advanced

specic preference le


val sharedPref = activity?.getSharedPreferences(getString(R.
string.preference_file_key), Context.MODE_PRIVATE)

Encrypted shared prefenreces:


EncryptedSharedPreferences, to store sensitive data
DataStore: store any typed object not only the primitive types
Data Storage in Android
Files

Types

Internal storage directories: These directories include both a


dedicated location for storing persistent les, and another
location for storing cache data.
External storage directories: These directories include both a
dedicated location for storing persistent les, and another
location for storing cache data. (Needs a permission)
Data Storage in Android
Files

Access les

val filename = "myfile"


val fileContents = "Hello world!"
context.openFileOutput(filename, Context.MODE_PRIVATE).use {
it.write(fileContents.toByteArray())
}
Data Storage in Android
Database

Types

SQLiteOpenHelper
Room
LivingRoom

You might also like