0% found this document useful (0 votes)
16 views36 pages

09.0 Data Storage

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

09.0 Data Storage

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

Android Developer Fundamentals V2

Preferences
and settings

Lesson 9

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 1
Fundamentals V2 International License
9.0 Data Storage

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 2
Fundamentals V2 International License
Contents

● Android File System


● Internal Storage
● External Storage
● SQLite Database
● Other Storage Options

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 3
Fundamentals V2 International License
Storage
Options

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 4
Fundamentals V2 International License
Storing data

● Shared Preferences—Private primitive data in key-value pairs


● Internal Storage—Private data on device memory
● External Storage—Public data on device or external storage
● SQLite Databases—Structured data in a private database
● Content Providers—Store privately and make available
publicly

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 5
Fundamentals V2 International License
Storing data beyond Android

● Network Connection—On the web with your own


server
● Cloud Backup—Back up app and user data in the
cloud
● Firebase Realtime Database—Store and sync data
with NoSQL cloud database across clients in
realtime
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 6
Fundamentals V2 International License
Files

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 7
Fundamentals V2 International License
Android File System

● External storage -- Public directories


● Internal storage -- Private directories for just your
app

Apps can browse the directory structure


Structure and operations similar to Linux and java.io
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 8
Fundamentals V2 International License
Internal storage
● Always available
● Uses device's filesystem
● Only your app can access files, unless explicitly set
to be readable or writable
● On app uninstall, system removes all app's files
from internal storage

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 9
Fundamentals V2 International License
External storage

● Not always available, can be removed


● Uses device's file system or physically external
storage like SD card
● World-readable, so any app can read
● On uninstall, system does not remove files private
to app

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 10
Fundamentals V2 International License
When to use internal/external
storage
Internal is best when
● you want to be sure that neither the user nor other
apps can access your files
External is best for files that
● don't require access restrictions and for
● you want to share with other apps
● you allow the user to access with a computer
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 11
Fundamentals V2 International License
Save user's file in shared
storage
● Save new files that the user acquires through your
app to a public directory where other apps can
access them and the user can easily copy them
from the device
● Save external files in public directories

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 12
Fundamentals V2 International License
Internal
Storage

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 13
Fundamentals V2 International License
Internal Storage

● Uses private directories just for your app


● App always has permission to read/write
● Permanent storage directory—getFilesDir()
● Temporary storage directory—getCacheDir()

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 14
Fundamentals V2 International License
Creating a file

File file = new File(


context.getFilesDir(), filename);

Use standard java.io file operators or streams


to interact with files

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 15
Fundamentals V2 International License
External
Storage

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 16
Fundamentals V2 International License
External Storage
● On device or SD card
● Set permissions in Android Manifest
○ Write permission includes read permission

<uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission

This work is licensed under a


android:name="android.permission.READ_EXTERNAL_STORAGE"
Android Developer Data storage Creative Commons Attribution 4.0
International License
/> 17
Fundamentals V2
Always check availability of
storage
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 18
Fundamentals V2 International License
Example external public
directories
● DIRECTORY_ALARMS and DIRECTORY_RINGTONES
For audio files to use as alarms and ringtones
● DIRECTORY_DOCUMENTS
For documents that have been created by the user
● DIRECTORY_DOWNLOADS
For files that have been downloaded by the user
developer.android.com/reference/android/os/Environment.h
tml This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 19
Fundamentals V2 International License
Accessing public external
directories
1. Get a path getExternalStoragePublicDirectory()
2. Create file

File path = Environment.getExternalStoragePublicDirectory(


Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 20
Fundamentals V2 International License
How much storage left?
● If there is not enough space, throws IOException
● If you know the size of the file, check against
space
○ getFreeSpace()
○ getTotalSpace().
● If you do not know how much space is needed
○ try/catch IOException
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 21
Fundamentals V2 International License
Delete files no longer needed

● External storage
myFile.delete();

● Internal storage
myContext.deleteFile(fileName);

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 22
Fundamentals V2 International License
Do not delete the user's files!
When the user uninstalls your app, your app's private
storage directory and all its contents are deleted
Do not use private storage for content that
belongs to the user!
For example
● Photos captured or edited with your app
● Music the user has purchased with your app
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 23
Fundamentals V2 International License
Shared
Preferences
& SQLite
Database
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 24
Fundamentals V2 International License
SQLite Database

● Ideal for repeating or structured data, such as


contacts
● Android provides SQL-like database
● Covered in following chapters and practicals
○ SQLite Primer
○ Introduction to SQLite Databases
○ SQLite Data Storage Practical
○ Searching an SQLite Database Practical
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 25
Fundamentals V2 International License
Shared Preferences

● Read and write small amounts of primitive data as


key/value pairs to a file on the device storage

● Covered in later chapter and practical


○ Shared Preferences

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 26
Fundamentals V2 International License
Other
Storage
Options

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 27
Fundamentals V2 International License
Use Firebase to store and share
data
Store and sync data with the
Firebase cloud database
Data is synced across all clients,
and remains available when your
app goes offline
firebase.google.com/docs/databas
e/
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 28
Fundamentals V2 International License
Firebase Realtime Database

● Connected apps share data


● Hosted in the cloud
● Data is stored as JSON
● Data is synchronized in realtime
to every connected client

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 29
Fundamentals V2 International License
Network Connection

● You can use the network (when it's available) to


store and retrieve data on your own web-based
services

● Use classes in the following packages


○ java.net.*
○ android.net.*
This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 30
Fundamentals V2 International License
Backing up data

● Auto Backup for 6.0 (API level 23) and higher


● Automatically back up app data to the cloud
● No code required and free
● Customize and configure auto backup for your app
● See Configuring Auto Backup for Apps

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 31
Fundamentals V2 International License
Backup API for Android 5.1 (API level
22)
1. Register for Android Backup Service to get a Backup Service
Key
2. Configure Manifest to use Backup Service
3. Create a backup agent by extending the BackupAgentHelper
class
4. Request backups when data has changed
More info and sample code: Using the Backup AP and
Data Backup This work is licensed under a
Android Developer Data storage Creative Commons Attribution 4.0 32
Fundamentals V2 International License
Learn more about files

● Saving Files
● getExternalFilesDir() documentation and code samples
● getExternalStoragePublicDirectory() documentation and code s
amples
● java.io.File class
● Oracle's Java I/O Tutorial

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 33
Fundamentals V2 International License
Learn more about backups

● Configuring Auto Backup for Apps


● Using the Backup API
● Data Backup

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 34
Fundamentals V2 International License
What's next?

● Concept Chapter: 9.0 Data Storage


● No practical, this was an overview lecture

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 35
Fundamentals V2 International License
END

This work is licensed under a


Android Developer Data storage Creative Commons Attribution 4.0 36
Fundamentals V2 International License

You might also like