KEMBAR78
MobApp 13 - Persistent Data | PDF | Mobile App | Computer Data Storage
0% found this document useful (0 votes)
25 views37 pages

MobApp 13 - Persistent Data

Uploaded by

arslanqadri958
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)
25 views37 pages

MobApp 13 - Persistent Data

Uploaded by

arslanqadri958
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/ 37

Mobile Application Development Arfan Shahzad

{ arfanskp@gmail.com }
Mobile Application Development
Persistent Data

• Persistent data denotes information that is infrequently accessed and


not likely to be modified.

• Android uses a file system that's similar to disk-based file systems on


other platforms.

• The system provides several options for you to save your app data:
Persistent Data cont…

1. App-specific storage

2. Shared storage

3. Preferences

4. Databases
Persistent Data cont…
App-specific Storage
• Store files that are meant for your app's use only, either in dedicated
directories within an internal storage volume or different dedicated
directories within external storage.

• Use the directories within internal storage to save sensitive


information that other apps shouldn't access.
Persistent Data cont…
Shared Storage
• Store files that your app intends to share with other apps, including
media, documents, and other files.
Persistent Data cont…
Preferences
• Store private, primitive data in key-value pairs.
Persistent Data cont…
Databases
• Store structured data in a private database using the Room
persistence library.

• The characteristics of these options are summarized in the following


table:
Persistent Data cont…
Characteristics
Files
Type of removed on
Access method Permissions needed Can other apps access?
content app
uninstall?
From internal Never needed for internal
App- Files meant for storage, getFilesDir() or getCac storage
specific your app's use heDir()
files only
From external Not needed for external No
storage, getExternalFilesDir() o storage when your app is
r getExternalCacheDir() used on devices that run
Android 4.4 (API level 19)
or higher
Persistent Data cont…
Characteristics
Type of Permissions Files removed on
Access method Can other apps access?
content needed app uninstall?
READ_EXTERNAL_STORAGE when
Shareable accessing other apps' files on Android 11
Media media files MediaStore (API level 30) or higher Yes, though the
(images, audio API other app needs
files, videos) READ_EXTERNAL_STORAGE or the
WRITE_EXTERNAL_STORAGE when READ_EXTERNAL
accessing other apps' files on Android 10 _STORAGE
(API level 29) Permission

Permissions are required for all files on


Android 9 (API level 28) or lower
Persistent Data cont…
Characteristics
Files
Type of removed on
Access method Permissions needed Can other apps access?
content app
uninstall?
Documents Other types of Storage Access None Yes, through
and other files shareable content, Framework the system
including file picker
downloaded files
Persistent Data cont…
Characteristics
Files
Type of removed on
Access method Permissions needed Can other apps access?
content app
uninstall?
Documents Other types of Storage Access None Yes, through
and other files shareable content, Framework the system
including file picker
downloaded files
App Key-value pairs Jetpack None No
preferences Preferences library
Persistent Data cont…
Characteristics
Files
Type of removed on
Access method Permissions needed Can other apps access?
content app
uninstall?
Documents Other types of Storage Access None Yes, through
and other files shareable content, Framework the system
including file picker
downloaded files
App Key-value pairs Jetpack None No
preferences Preferences library
Database Structured data Room persistence library None No
Persistent Data cont…
How to choose appropriate Solution?
• The solution you choose depends on your specific needs:

• 1- How much space does your data require?

• Internal storage has limited space for app-specific data.

• Use other types of storage if you need to save a substantial amount of


data.
Persistent Data cont…
How to choose appropriate Solution?
• 2- How reliable does data access need to be?

• If your app's basic functionality requires certain data, such as when your
app is starting up, place the data within internal storage directory or a
database.

• App-specific files that are stored in external storage aren't always reliable
and accessible because some devices allow users to remove a physical
device that corresponds to external storage.
Persistent Data cont…
How to choose appropriate Solution?
• 3- What kind of data do you need to store?
• If you have data that's only meaningful for your app, use app-specific
storage.
• For shareable media content, use shared storage so that other apps can
access the content.
• For structured data, use either preferences (for key-value data) or a
database (for data that contains more than 2 columns).
Persistent Data cont…
How to choose appropriate Solution?
• 4- Should the data be private to your app?

• When storing sensitive data—data that shouldn't be accessible from


any other app—use internal storage, preferences, or a database.

• Internal storage has the added benefit of the data being hidden from
users.
Persistent Data cont…
Categories of storage locations
• Android provides two types of physical storage locations:

1. Internal storage and

2. External storage.
Persistent Data cont…
Categories of storage locations
• On most devices, internal storage is smaller than external storage.

• However, internal storage is always available on all devices, making it


a more reliable place to put data on which your app depends.

• Removable volumes, such as an SD card, appear in the file system as


part of external storage.
Persistent Data cont…
Categories of storage locations
• Android represents these devices using a path, such as /sdcard.

• Caution: The exact location of where your files can be saved might
vary across devices. For this reason, don't use hard-coded file paths.
Persistent Data cont…
Categories of storage locations
• Apps themselves are stored within internal storage by default.

• If your APK size is very large, however, you can indicate a preference
within your app's manifest file to install your app on external storage
instead:
Persistent Data cont…
Permissions and access to external storage
• Android defines the following storage-related permissions:

• READ_EXTERNAL_STORAGE,

• WRITE_EXTERNAL_STORAGE, and

• MANAGE_EXTERNAL_STORAGE.
Persistent Data cont…
Shared Preferences
• The SharedPreferences class allows you to save and retrieve key /
value pairs of primitive data types.

• We can use the SharedPreferences to save the primitive data:


booleans, floats, ints, longs, and strings.

• These data will persist in the user session (even if your application is
dead).
Persistent Data cont…
Shared Preferences
• For an SharedPreferences object to your application, use one of two
methods:

1. getSharedPreferences (String name, int mode): Use if you need several


preferences files identified by name that will be passed in the first
parameter.

2. getPreferences (int mode) - Use if you just need a preferences file for
your activity.
Persistent Data cont…
Shared Preferences
• To write values:

1. Call the method edit() to get a SharedPreferences.Editor;

2. Add values methods such as putBoolean() and putString();

3. Persists the new values with commit().

4. To read SharedPreferences values use the methods as getBoolean()


and getString().
Persistent Data cont…
Shared Preferences
• public static final String PREFS_NAME = "MyPrefsFile";

• private boolean test;

• private void store(){

• SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

• SharedPreferences.Editor editor = settings.edit();

• editor.putBoolean("test", test);

• // Commit editings editor.commit();

• } private void recover(){ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); test =


settings.getBoolean("test", false); }
Persistent Data cont…
Shared Preferences
Persistent Data cont…
Internal Storage
• You can save files directly on the device's internal memory.

• By default, files saved to the internal storage are deprived of their


application, allowing other applications can not access them.

• When the user uninstalls the application, these files are removed.
Persistent Data cont…
Internal Storage
• To create and save a private file to the internal storage:

1. Call openFileOutput () with the file name and the operating mode
(in case MODE_PRIVATE). This returns a FileOutputStream;

2. Write on file with the write ();

3. Close the stream with close ().


Persistent Data cont…
Internal Storage
Persistent Data cont…
External storage
• Each Android-compatible device supports an "external memory" shared
that you can use to save files.

• This may be removable storage media (such as an SD card) or an internal


memory (not removable).

• Files saved to the external storage are reading for all and can be modified
by the user when they allow USB mass storage to transfer files from a
computer.
Persistent Data cont…
External storage
• Before doing any work with the external storage, you should always
call Environment.getExternalStorageState () to check that the media is
available.

• The media can be mounted to a computer, missing, read-only, or in


some other state.
Persistent Data cont…
External storage
Persistent Data cont…
External storage
Persistent Data cont…
Offline Databases
• A database is very useful for any large or small system, unless your
system deals only with simple data, without using a bank to store
information.
Persistent Data cont…
Online Databases
• There are various services offered online such as storage, online
processing, realtime database, authorisation of user etc.

• Google developed a platform called Firebase that provide all these


online services.

• It also gives a daily analysis of usage of these services along with the
details of user using it.
Persistent Data cont…
Online Databases
• To simplify, it can be said that Firebase is a mobile and web
application development platform.

• It provides services that a web application or mobile application


might require.

• Anyone can easily include firebase to there application and it will


make their online work way easier than it was used to be.

You might also like