0% found this document useful (0 votes)
2 views

3 Memory Android

Uploaded by

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

3 Memory Android

Uploaded by

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

Data and file storage overview

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:
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.
Shared storage:
Store files that your app intends to share with other apps, including
media, documents, and other files.

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.

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 accessible because some devices allow users
to remove a physical device that corresponds to external storage.

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).

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.

Categories of storage locations


Android provides two types of physical storage locations: internal
storage and external storage. 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. Android represents these devices using a
path, such as /sdcard.

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:
<manifest ...
android:installLocation="preferExternal">
...
</manifest>

Data storage
Android provides several options for you to save persistent app data. The
solution you choose depends on your specific needs, such as whether the
data should be private to your app or accessible to other apps (and the
user) and how much space your data requires.
Your data storage options include the following:

 Shared preferences: Store private primitive data in key-value pairs. This


is covered in the next chapter.
 Internal storage: Store private data on the device memory.
 External storage: Store public data on the shared external storage.
 SQLite databases: Store structured data in a private database.
 Room persistence library: Part of the Android Architecture
Component libraries. Room caches an SQLite database locally, and
automatically syncs changes to a network database
 Cloud backup: Back up your app and user data in the cloud.
 Firebase realtime database: Store and sync data with a NoSQL cloud
database. Data is synced across all clients in real time, and remains
available when your app goes offline.

Files
Android uses a file system that's similar to disk-based file systems on
other platforms such as Linux. File-based operations should be familiar to
anyone who has used Linux file I/O or the java.io package.
All Android devices have two file storage areas: "internal" and "external"
storage. These names come from the early days of Android, when most
devices offered built-in non-volatile memory (internal storage), plus a
removable storage medium such as a micro SD card (external storage).
Today, some devices divide the permanent storage space into "internal"
and "external" partitions, so even without a removable storage medium,
there are always two storage spaces and the API behavior is the same
whether the external storage is removable or not. The following lists
summarize the facts about each storage space.

Internal storage External storage

Always available. Not always available, because the


user can mount the external storage
as USB storage and in some cases
remove it from the device.

Only your app can access files. World-readable. Any app can read.
Specifically, your app's internal storage
directory is specified by your app's
package name in a special location of
the Android file system. Other apps
cannot browse your internal directories
and do not have read or write access
unless you explicitly set the files to be
readable or writable.

When the user uninstalls your app, the When the user uninstalls your app,
system removes all your app's files the system removes your app's files
from internal storage. from here only if you save them in
the directory
from getExternalFilesDir().
Internal storage is best when you want External storage is the best place
to be sure that neither the user nor for files that don't require access
other apps can access your files. restrictions and for files that you
want to share with other apps or
allow the user to access with a
computer.

Internal storage
You don't need any permissions to save files on the internal storage. Your
app always has permission to read and write files in its internal storage
directory.

External storage
Use external storage for files that should be permanently stored, even if
your app is uninstalled, and be available freely to other users and apps,
such as pictures, drawings, or documents made by your app.
Some private files that are of no value to other apps can also be stored
on external storage. Such files might be additional downloaded app
resources, or temporary media files. Make sure you delete those when
your app is uninstalled.

Public and private external storage


External storage is very specifically structured by the Android system for
various purposes. There are public and private directories specific to your
app. Each of these file trees has directories identified by system
constants.
For example, any files that you store into the public ringtone
directory DIRECTORY_RINGTONES are available to all other ringtone apps.
On the other hand, any files you store in a private ringtone
directory DIRECTORY_RINGTONES can, by default, only be seen by your app
and are deleted along with your app.
Source:

https://google-developer-training.github.io/android-developer-fundamentals-
course-concepts-v2/unit-4-saving-user-data/lesson-10-storing-data-with-room/
10-0-c-sqlite-primer/10-0-c-sqlite-primer.html

SQL databases
 SQL databases store data in tables of rows and columns:
 The intersection of a row and column is called a field.
 Fields contain data, references to other fields, or references to other
tables.
 Rows are identified by unique IDs.
 Columns are identified by names that are unique per table.
 Think of a database as a spreadsheet with rows, columns, and cells,
where cells can contain data, references to other cells, and links to
other sheets.
Database for Android App
Where can you save app data?
 Locally
 Files on SD Card
 SQLite Database
 Cloud
API Services
SQL Server (MS SQL, MySQL etc)
Google Firebase, Microsoft Azure, Mongo DB
SQLite
SQLite is a software library. SQLite implements an SQL database engine
that has the following characteristics:
 Self-contained (requires no other components)
 Serverless (requires no server backend)
 Zero-configuration (does not need to be configured for your app)
 Transactional (changes within a single transaction in SQLite either
occur completely or not at all)
SQLite is the most widely deployed database engine in the world. The
source code for SQLite is in the public domain.

SQLite Database:
SQLite is a software library that provides a relational database
management system. The lite in SQLite means lightweight in terms of
setup, database administration, and required resources.

SQLite has the following noticeable features: self-contained, serverless,


zero-configuration, transactional.
Serverless

Normally, an RDBMS such as MySQL, PostgreSQL, etc., requires a separate


server process to operate. The applications that want to access the
database server use TCP/IP protocol to send and receive requests. This is
called client/server architecture.

The following diagram illustrates the RDBMS client/server architecture:

SQLite does NOT work this way.

SQLite does NOT require a server to run.

SQLite database is integrated with the application that accesses the


database. The applications interact with the SQLite database read and
write directly from the database files stored on disk.

The following diagram illustrates the SQLite server-less architecture:

Self-Contained
SQLite is self-contained means it requires minimal support from the operating
system or external library. This makes SQLite usable in any environment
especially in embedded devices like iPhones, Android phones, game consoles,
handheld media players, etc.

SQLite is developed using ANSI-C. The source code is available as a big sqlite3.c
and its header file sqlite3.h. If you want to develop an application that uses
SQLite, you just need to drop these files into your project and compile it with
your code.

Zero-configuration

Because of the serverless architecture, you don’t need to “install” SQLite before
using it. There is no server process that needs to be configured, started, and
stopped.

In addition, SQLite does not use any configuration files.

Transactional

All transactions in SQLite are fully ACID-compliant. It means all queries and
changes are Atomic, Consistent, Isolated, and Durable.

In other words, all changes within a transaction take place completely or not at
all even when an unexpected situation like application crash, power failure, or
operating system crash occurs.

Transactions
A transaction is a sequence of operations performed as a single
logical unit of work. A logical unit of work must exhibit four
properties, called the atomicity, consistency, isolation, and durability
(ACID) properties, to qualify as a transaction:

 Atomicity. Either all of a transaction's data modifications are


performed, or none of them are performed. This is true even if
the act of writing the change to the disk is interrupted by a
program crash, operating system crash, or power failure.
 Consistency. When completed, a transaction must leave all data
in a consistent state.
 Isolation. Modifications made by concurrent transactions must
be isolated from the modifications made by any other
concurrent transactions. A transaction either recognizes data in
the state it was in before another concurrent transaction
modified it, or it recognizes the data after the second
transaction has completed, but it does not recognize an
intermediate state.
 Durability. After a transaction has completed, its effects are
permanently in place in the system. The modifications persist
even in the event of a system failure.

Examples of transactions:
Transferring money from a savings account to a checking account.

More on SQLite:
SQLite is an open source SQL database that stores data to a text file on a device.
Android comes in with built in SQLite database implementation.

SQLite is a C-language library that implements a small, fast, self-contained, high-


reliability, full-featured, SQL database engine. SQLite is the most used database
engine in the world. SQLite is built into all mobile phones and most computers
and comes bundled inside countless other applications that people use every
day.

SQLite supports all the relational database features. In order to access this
database, you don't need to establish any kind of connections for it like
JDBC,ODBC e.t.c

The main package is android.database.sqlite that contains the classes to manage


your own database

SQLite is an embedded SQL database engine. Unlike most other SQL databases,
SQLite does not have a separate server process. SQLite reads and writes directly
to ordinary disk files. A complete SQL database with multiple tables, indices,
triggers, and views, is contained in a single disk file. The database file format is
cross-platform - you can freely copy a database between 32-bit and 64-bit
systems. These features make SQLite a popular choice as an Application File
Format.

You might also like