SlideShare a Scribd company logo
1 of 41
Download to read offline
Лекція 6. Робота з даними
на мобільних пристроях
M.V.Davydov
Lviv Polytechnic National University
Storage options
Shared Preferences
Store private primitive data in key-value pairs.
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.
Network Connection
Store data on the web with your own network server.
Use content providers to expose data to other aplications
SharedPreferences
SharedPreferences варто
використовувати для зберігання
примітивних даних: booleans, floats, ints,
longs, and strings.
Ці дані будуть збережені між
користувацькими сесіями (навіть, якщо
процес програми буде аварійно завершено).
Отримання об’єкту
SharedPreferences
To get a SharedPreferences object for your
application, use one of two methods:
• getSharedPreferences() - Use this if you need
multiple preferences files identified by name, which
you specify with the first parameter.
• getPreferences() - Use this if you need only one
preferences file for your Activity. Because this will
be the only preferences file for your Activity, you
don't supply a name.
Редагування
SharedPreferences
To write values:
1. Call edit() to get a SharedPreferences.Editor.
2. Add values with methods such as putBoolean()
and putString().
3. Commit the new values with commit()
Приклад використання
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
SharedPreferences and
UserPreferences
Для автоматизації редагування і збереження користувацьких
налаштувань зручніше використовувати PreferenceActivity.
Shared preferences are not strictly for saving "user preferences," such
as what ringtone a user has chosen.
If you're interested in creating user preferences for your application, see
PreferenceActivity, which provides an Activity framework for
you to create user preferences, which will be automatically persisted
(using shared preferences).
Preference Activity example (from http://
alvinalexander.com/android/android-tutorial-
preferencescreen-preferenceactivity-preferencefragment)
1 Define the UI in XML, using a PreferenceScreen as the
main component/container
2 As an optional step, populate some Android string arrays to
use with your preferences components
3 Write a PreferenceActivity and PreferenceFragment
in Java code to show/handle the UI
The res/xml/preferences.xml file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/
res/android">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Введіть тут своє
ім&apos;я"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected
will allow the application to check for latest versions."
android:key="applicationUpdates" />
<ListPreference android:title="Download Details"
android:summary="Select the kind of data
that you would like to download"
android:key="downloadType"
android:defaultValue="1"
android:entries="@array/listArray"
android:entryValues="@array/listValues" />
</PreferenceScreen>
The res/values/array.xml file
<resources>
<string-array name="listArray">
<item>Headings</item>
<item>Headings and Details</item>
<item>All Data</item>
</string-array>
<string-array name="listValues">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
Activity code
package com.alvinalexander.preferencestestapp;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
Результат
Java code to launch the
PreferenceActivity
Intent i = new Intent(
this, MyPreferencesActivity.class);
startActivity(i);
Доступ до налаштувань з інших
частин коду
SharedPreferences SP =
PreferenceManager.getDefaultSharedPreferences(
getBaseContext());
String strUserName =
SP.getString("username", "NA");
boolean bAppUpdates =
SP.getBoolean("applicationUpdates",false);
String downloadType =
SP.getString("downloadType","1");
Переваги і недоліки налаштувань
+ дані можуть бути видимі або невидимі іншим програмам, в
залежності від режиму доступу (якщо пристій"рутований", то
ці дані можна прочитати з іншої програми)
+зручно зберігати елементарні дані
+підтримується атомарність внесення змін
- не зручно використовувати для зберігання структурованих
даних
- можуть не видалятися при видалені програми на деяких
пристроях або архівуватися, якщо в маніфесті програми не
вказано <application … android:allowBackup="false">...</
application>
Using the Internal Storage.
Запис файлу у внутрішнє сховище
You can save files directly on the device's internal storage. By
default, files saved to the internal storage are private to your
application and other applications cannot access them (nor can
the user). When the user uninstalls your application, these files are
removed.
To create and write a private file to the internal storage:
1. Call openFileOutput() with the name of the file and the
operating mode. This returns a FileOutputStream.
2. Write to the file with write().
3. Close the stream with close().
Приклад запису файлу
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME,
Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
MODE_PRIVATE створить файл (або замінить файл з тим
самим ім’ям) і зробить файл приватним для вашої
програми. Інші режими: MODE_APPEND,
MODE_WORLD_READABLE, and
MODE_WORLD_WRITEABLE.
Читання файлу
1. Call openFileInput() and pass it the name of the file
to read. This returns a FileInputStream.
2. Read bytes from the file with read().
3. Then close the stream with close().
Переваги і недоліки використання
внутрішнього сховища
+ дані не видимі для читання іншим програмам (доки
пристій не "рутований" (rooted) або явно не вказано
MODE_WORLD_READABLE або
MODE_WORLD_WRITEABLE)
+ можна зберігати дані у будь-якому форматі
- користувачі завжди жаліються на нестачу внутрішньої
пам’яті і просять її не використовувати для збереження
великих файлів
-контроль атомарності і цілісності цілком покладається на
програміста
Робота з тимчасовими файлами
• If you'd like to cache some data, rather than store it
persistently, you should use getCacheDir() to open a
File that represents the internal directory where your
application should save temporary cache files.
• When the device is low on internal storage space,
Android may delete these cache files to recover
space. However, you should not rely on the system
to clean up these files for you. You should always
maintain the cache files yourself and stay within a
reasonable limit of space consumed, such as 1MB.
When the user uninstalls your application, these files
are removed.
Корисні методи
getFilesDir()
Gets the absolute path to the filesystem directory
where your internal files are saved.
getDir()
Creates (or opens an existing) directory within your
internal storage space.
deleteFile()
Deletes a file saved on the internal storage.
fileList()
Returns an array of files currently saved by your
application.
Using the External Storage
Every Android-compatible device supports a shared "external storage"
that you can use to save files. This can be a removable storage media
(such as an SD card) or an internal (non-removable) storage. Files
saved to the external storage are world-readable and can be modified by
the user when they enable USB mass storage to transfer files on a
computer.
Caution: External storage can become unavailable if the user mounts
the external storage on a computer or removes the media, and there's no
security enforced upon files you save to the external storage. All
applications can read and write files placed on the external storage and
the user can remove them.
Getting access to external storage
In order to read or write files on the external storage, your app must acquire
the READ_EXTERNAL_STORAGE or
WRITE_EXTERNAL_STORAGE system permissions. For example:
<manifest ...>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
If you need to both read and write files, then you need to request only the
WRITE_EXTERNAL_STORAGE permission, because it implicitly
requires read access as well.
Note: Beginning with Android 4.4, these permissions are not required if
you are reading or writing only files that are private to your app. For more
information, see the section below about saving files that are app-private.
Checking media availability with
getExternalStorageState()
Checking media availability with
getExternalStorageState()
/* Checks if external storage is available for read and
write */
public boolean isExternalStorageWritable() {
String state =
Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least
read */
public boolean isExternalStorageReadable() {
String state =
Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Saving files that can be shared with
other apps
Generally, new files that the user may acquire through your app
should be saved to a "public" location on the device where other
apps can access them and the user can easily copy them from the
device. When doing so, you should use to one of the shared public
directories, such as Music/, Pictures/, and Ringtones/.
To get a File representing the appropriate public directory, call
getExternalStoragePublicDirectory(), passing it the type of
directory you want, such as DIRECTORY_MUSIC,
DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. By
saving your files to the corresponding media-type directory, the
system's media scanner can properly categorize your files in the
system (for instance, ringtones appear in system settings as
ringtones, not as music).
Saving files that can be shared with other apps
(приклад отримання папки для збереження
зображень)
public File getAlbumStorageDir(String albumName)
{
File file = new
File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),
albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
Необхідні дозволи у версії SDK<=18
Beginning with Android 4.4, reading or writing files in your
app's private directories does not require the
READ_EXTERNAL_STORAGE or
WRITE_EXTERNAL_STORAGE permissions. So you can
declare the permission should be requested only on the
lower versions of Android by adding the maxSdkVersion
attribute:
<manifest ...>
    <uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
           android:maxSdkVersion="18" />
    ...
</manifest>
Saving files that are app-private
If you are handling files that are not intended for other
apps to use (such as graphic textures or sound effects
used by only your app), you should use a private storage
directory on the external storage by calling
getExternalFilesDir(). This method also takes a type
argument to specify the type of subdirectory (such as
DIRECTORY_MOVIES). If you don't need a specific
media directory, pass null to receive the root directory
of your app's private directory.
Ця папка буде видалена при деінсталяції програми, тому
зберігати там особисті файли користувача не варто
Важливо!
• На деяких платформах getExternalFilesDir()
може вертати папку з внутрішньої пам’яті. На цих
платформах можна використовувати фугкцію
getExternalFilesDirs() для отримання переліку
папок для збереження даних (для Android <=4.3
ContextCompat.getExternalFilesDirs())
• Інші програми можуть отримати доступ до файлів
до зовнішніх файлів програми, якщо вони мають
право доступу READ_EXTERNAL_STORAGE
Збереження тимчасових файлів на
зовнішньому носії
To open a File that represents the external storage directory
where you should save cache files, call getExternalCacheDir().
If the user uninstalls your application, these files will be
automatically deleted.
You can also access a cache directory on a secondary external
storage (if available) by calling
ContextCompat.getExternalCacheDirs().
Tip: To preserve file space and maintain your app's
performance, it's important that you carefully manage your
cache files and remove those that aren't needed anymore
throughout your app's lifecycle.
Переваги і недоліки зовнішнього
носія
+ зазвичай, має більше місця, ніж внутрішній носій
+ містить спеціальні папки для збереження файлів
користувача (картинки, відео, музика, …)
+ програма може доступитися до всіх файлів зовнішнього
носія з правом доступу READ_EXTERNAL_STORAGE
- треба перевіряти наявність зовнішнього сховища
- цілісність даних може бути порушена з різних причин
- важче забезпечити приватність даних (треба шифрувати)
Збереження даних у базі даних
Android provides full support for SQLite databases.
Бази даних не доступна для читання іншим програмам
The recommended method to create a new SQLite database is to
create a subclass of SQLiteOpenHelper and override the onCreate()
method, in which you can execute a SQLite command to create tables
in the database.
You can then get an instance of your SQLiteOpenHelper
implementation using the constructor you've defined. To write to and
read from the database, call getWritableDatabase() and
getReadableDatabase(), respectively. These both return a
SQLiteDatabase object that represents the database and provides
methods for SQLite operations.
Виконання запитів
• You can execute SQLite queries using the
SQLiteDatabase query() methods, which accept various
query parameters, such as the table to query, the
projection, selection, columns, grouping, and others. For
complex queries, such as those that require column
aliases, you should use SQLiteQueryBuilder, which
provides several convienent methods for building queries.
• Every SQLite query will return a Cursor that points to all
the rows found by the query. The Cursor is always the
mechanism with which you can navigate results from a
database query and read rows and columns.
Database debugging
The Android SDK includes a sqlite3 database tool
that allows you to browse table contents, run SQL
commands, and perform other useful functions on
SQLite databases. See Examining sqlite3 databases
from a remote shell to learn how to run this tool.
adb -s emulator-5554 shell
# sqlite3 /data/data/…/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
sqlite> .exit
Приклад використання бази даних
package ua.lpnu.testshop;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Vector;
import java.lang.String;
/**
* Created by maksymdavydov on 3/26/16.
*/
public class LocalDatabase extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "myitems";
private static final String ITEM_TABLE = "items";
private static final String ITEM_NAME_COL = "item_name";
private static final String ITEMS_TABLE_CREATE_SQL =
"CREATE TABLE "+ITEM_TABLE+" ( "+ITEM_NAME_COL+" TEXT )";
LocalDatabase(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(ITEMS_TABLE_CREATE_SQL);
for(int i=0;i<100;++i)
{
addItem("DatabaseItem" + i);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion)
{
// TODO: implement when several database
versions are available
}
public long addItem(String item_name)
{
ContentValues values = new ContentValues();
values.put(ITEM_NAME_COL, item_name);
return getWritableDatabase().insert
(ITEM_TABLE, null, values);
}
public Vector<String> getItems()
{
Vector<String> ret = new Vector<String>();
Cursor cursor = getReadableDatabase().rawQuery
("SELECT * FROM " + ITEM_TABLE, null);
try
{
for(cursor.moveToFirst();
!cursor.isLast(); cursor.moveToNext())
{
ret.add(new String(cursor.getString(0)));
}
}
finally
{
cursor.close();
}
return ret;
}
public void deleteItem(String item_name)
{
getWritableDatabase().delete(ITEM_TABLE,
ITEM_NAME_COL + " = ?", new String[] {item_name});
}
}
Переваги і недоліки
використання sqlite
+ структуровані дані, контроль за атомарністю
операцій, транзакції
+ зручний набір функцій
+ вбудована підтримка версій
- значно повільніше, ніж читати файл
- виконання SQL запитів вимагає обережної роботи
зі стрічками
Зберігання даних в мережі
(хмарі)
+ дані доступні з різних пристроїв
+ легше перевести користувача на підписку
- можливі затримки при читанні, збереженні
- складніше реалізувати - необхідно реалізувати
кешування, синхронізацію, тощо
Детальніше на наступній лекції

More Related Content

Similar to Android Storage

9 клас. Спільний доступ до папок
9 клас. Спільний доступ до папок9 клас. Спільний доступ до папок
9 клас. Спільний доступ до папокYulia Novichenko
 
локальна мережа
локальна мережалокальна мережа
локальна мережаaniadania
 
локальна мережа
локальна мережалокальна мережа
локальна мережаdan657
 
файли
файлифайли
файлиcit-cit
 
6 клас урок 9. Поняття файлової системи
6 клас урок 9. Поняття файлової системи6 клас урок 9. Поняття файлової системи
6 клас урок 9. Поняття файлової системиЮлія Артюх
 
Презентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєть
Презентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєтьПрезентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєть
Презентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєтьkostyasheremetyev1
 
5 клас урок 7
5 клас урок 75 клас урок 7
5 клас урок 7Olga Sokolik
 
Порівняння Drupal та Typo3
Порівняння Drupal та Typo3Порівняння Drupal та Typo3
Порівняння Drupal та Typo3Drupal Camp Kyiv
 
порівняння Drupal та Typo3
порівняння Drupal та Typo3порівняння Drupal та Typo3
порівняння Drupal та Typo3Inna Tuyeva
 
5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки
5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки
5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папкиkissoli
 
iPhone Objective-C Development (ukr) (2009)
iPhone Objective-C Development (ukr) (2009)iPhone Objective-C Development (ukr) (2009)
iPhone Objective-C Development (ukr) (2009)Anatoliy Okhotnikov
 
Informatyka-9-klas-Ryvkind-2022 (1).pdf
Informatyka-9-klas-Ryvkind-2022 (1).pdfInformatyka-9-klas-Ryvkind-2022 (1).pdf
Informatyka-9-klas-Ryvkind-2022 (1).pdfssuser59c0a2
 
informatyka_9_klas_ryvkind_2022.pdf
informatyka_9_klas_ryvkind_2022.pdfinformatyka_9_klas_ryvkind_2022.pdf
informatyka_9_klas_ryvkind_2022.pdfssuser59c0a2
 

Similar to Android Storage (20)

9 клас. Спільний доступ до папок
9 клас. Спільний доступ до папок9 клас. Спільний доступ до папок
9 клас. Спільний доступ до папок
 
презентація вчителя
презентація вчителяпрезентація вчителя
презентація вчителя
 
Razdel6
Razdel6Razdel6
Razdel6
 
Razdel6
Razdel6Razdel6
Razdel6
 
локальна мережа
локальна мережалокальна мережа
локальна мережа
 
локальна мережа
локальна мережалокальна мережа
локальна мережа
 
файли
файлифайли
файли
 
6 клас урок 4
6 клас урок 46 клас урок 4
6 клас урок 4
 
6 клас урок 9. Поняття файлової системи
6 клас урок 9. Поняття файлової системи6 клас урок 9. Поняття файлової системи
6 клас урок 9. Поняття файлової системи
 
Презентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєть
Презентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєтьПрезентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєть
Презентація 10 клас Урок 18 для учнів 10 класу вааааааааууу ахуєть
 
Less10
Less10Less10
Less10
 
5 клас урок 7
5 клас урок 75 клас урок 7
5 клас урок 7
 
Порівняння Drupal та Typo3
Порівняння Drupal та Typo3Порівняння Drupal та Typo3
Порівняння Drupal та Typo3
 
порівняння Drupal та Typo3
порівняння Drupal та Typo3порівняння Drupal та Typo3
порівняння Drupal та Typo3
 
6
66
6
 
5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки
5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки
5 клас урок 9 14 робочий стіл та його об'єкти. файли ,папки
 
iPhone Objective-C Development (ukr) (2009)
iPhone Objective-C Development (ukr) (2009)iPhone Objective-C Development (ukr) (2009)
iPhone Objective-C Development (ukr) (2009)
 
Informatyka-9-klas-Ryvkind-2022 (1).pdf
Informatyka-9-klas-Ryvkind-2022 (1).pdfInformatyka-9-klas-Ryvkind-2022 (1).pdf
Informatyka-9-klas-Ryvkind-2022 (1).pdf
 
середовище Lazarus
середовище Lazarusсередовище Lazarus
середовище Lazarus
 
informatyka_9_klas_ryvkind_2022.pdf
informatyka_9_klas_ryvkind_2022.pdfinformatyka_9_klas_ryvkind_2022.pdf
informatyka_9_klas_ryvkind_2022.pdf
 

More from Maksym Davydov

Microsoft mobile services
Microsoft mobile servicesMicrosoft mobile services
Microsoft mobile servicesMaksym Davydov
 
Mobile app design feature development
Mobile app design feature developmentMobile app design feature development
Mobile app design feature developmentMaksym Davydov
 
Android mix Java and C++
Android mix Java and C++Android mix Java and C++
Android mix Java and C++Maksym Davydov
 
Handler declaration in layout
Handler declaration in layoutHandler declaration in layout
Handler declaration in layoutMaksym Davydov
 
Interface Programming Android
Interface Programming AndroidInterface Programming Android
Interface Programming AndroidMaksym Davydov
 
Android Programming Intro
Android Programming IntroAndroid Programming Intro
Android Programming IntroMaksym Davydov
 
Lecture 02 Mobile hardware
Lecture 02 Mobile hardwareLecture 02 Mobile hardware
Lecture 02 Mobile hardwareMaksym Davydov
 
Lecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsLecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsMaksym Davydov
 
Lecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesLecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesMaksym Davydov
 
Lecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsLecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsMaksym Davydov
 
Lecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesLecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesMaksym Davydov
 
Lecture 11 Firebase overview
Lecture 11 Firebase overviewLecture 11 Firebase overview
Lecture 11 Firebase overviewMaksym Davydov
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesMaksym Davydov
 

More from Maksym Davydov (20)

Firebase overview
Firebase overviewFirebase overview
Firebase overview
 
Microsoft mobile services
Microsoft mobile servicesMicrosoft mobile services
Microsoft mobile services
 
Design of mobile apps
Design of mobile appsDesign of mobile apps
Design of mobile apps
 
Mobile app design feature development
Mobile app design feature developmentMobile app design feature development
Mobile app design feature development
 
Android mix Java and C++
Android mix Java and C++Android mix Java and C++
Android mix Java and C++
 
Android animations
Android animationsAndroid animations
Android animations
 
Handler declaration in layout
Handler declaration in layoutHandler declaration in layout
Handler declaration in layout
 
Android Networking
Android NetworkingAndroid Networking
Android Networking
 
Interface Programming Android
Interface Programming AndroidInterface Programming Android
Interface Programming Android
 
Java Small Tests
Java Small TestsJava Small Tests
Java Small Tests
 
Android Programming Intro
Android Programming IntroAndroid Programming Intro
Android Programming Intro
 
Lecture 02 Mobile hardware
Lecture 02 Mobile hardwareLecture 02 Mobile hardware
Lecture 02 Mobile hardware
 
Lecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsLecture 01 Mobile operating systems
Lecture 01 Mobile operating systems
 
Lecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesLecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile Devices
 
Lecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsLecture 12. iOS and Android Animations
Lecture 12. iOS and Android Animations
 
Lecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesLecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile services
 
Lecture 11 Firebase overview
Lecture 11 Firebase overviewLecture 11 Firebase overview
Lecture 11 Firebase overview
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
 
Lecture 08 Xamarin
Lecture 08 XamarinLecture 08 Xamarin
Lecture 08 Xamarin
 
Lecture 07 swift
Lecture 07 swiftLecture 07 swift
Lecture 07 swift
 

Recently uploaded

Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»
Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»
Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»tetiana1958
 
Презентациія для сайта Група «Незабудка».pptx
Презентациія для сайта Група «Незабудка».pptxПрезентациія для сайта Група «Незабудка».pptx
Презентациія для сайта Група «Незабудка».pptxOlgaDidenko6
 
Р.Шеклі "Запах думки". Аналіз оповідання
Р.Шеклі "Запах думки". Аналіз оповіданняР.Шеклі "Запах думки". Аналіз оповідання
Р.Шеклі "Запах думки". Аналіз оповіданняAdriana Himinets
 
Хімічні елементи в літературних творах 8 клас
Хімічні елементи в літературних творах 8 класХімічні елементи в літературних творах 8 клас
Хімічні елементи в літературних творах 8 класkrementsova09nadya
 
Застосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdf
Застосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdfЗастосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdf
Застосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdfssuser15a891
 
Бібліотека – розвиток дитячої творчості та дозвілля для дітейpptx
Бібліотека – розвиток дитячої творчості  та дозвілля для дітейpptxБібліотека – розвиток дитячої творчості  та дозвілля для дітейpptx
Бібліотека – розвиток дитячої творчості та дозвілля для дітейpptxssuserc301ed1
 
upd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdf
upd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdfupd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdf
upd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdfssuser54595a
 
Відкрита лекція на тему «Біологічний захист рослин у теплицях»
Відкрита лекція на тему «Біологічний захист рослин у теплицях»Відкрита лекція на тему «Біологічний захист рослин у теплицях»
Відкрита лекція на тему «Біологічний захист рослин у теплицях»tetiana1958
 
Принципові відмінності досконалої (повної) конкуренції від інших форм організ...
Принципові відмінності досконалої (повної) конкуренції від інших форм організ...Принципові відмінності досконалої (повної) конкуренції від інших форм організ...
Принципові відмінності досконалої (повної) конкуренції від інших форм організ...JurgenstiX
 
О.Духнович - пророк народної правди. Біографія
О.Духнович - пророк народної правди. БіографіяО.Духнович - пророк народної правди. Біографія
О.Духнович - пророк народної правди. БіографіяAdriana Himinets
 
Бомбочки для ванни своїми руками презентація
Бомбочки для ванни своїми руками презентаціяБомбочки для ванни своїми руками презентація
Бомбочки для ванни своїми руками презентаціяssuser0a4f48
 

Recently uploaded (13)

Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»
Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»
Відкрита лекція на тему «Контроль бур'янів в посівах соняшника»
 
Презентациія для сайта Група «Незабудка».pptx
Презентациія для сайта Група «Незабудка».pptxПрезентациія для сайта Група «Незабудка».pptx
Презентациія для сайта Група «Незабудка».pptx
 
Р.Шеклі "Запах думки". Аналіз оповідання
Р.Шеклі "Запах думки". Аналіз оповіданняР.Шеклі "Запах думки". Аналіз оповідання
Р.Шеклі "Запах думки". Аналіз оповідання
 
Хімічні елементи в літературних творах 8 клас
Хімічні елементи в літературних творах 8 класХімічні елементи в літературних творах 8 клас
Хімічні елементи в літературних творах 8 клас
 
Застосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdf
Застосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdfЗастосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdf
Застосування Гайду безбар’єрності в роботі закладів культури громад Одещини.pdf
 
Бібліотека – розвиток дитячої творчості та дозвілля для дітейpptx
Бібліотека – розвиток дитячої творчості  та дозвілля для дітейpptxБібліотека – розвиток дитячої творчості  та дозвілля для дітейpptx
Бібліотека – розвиток дитячої творчості та дозвілля для дітейpptx
 
upd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdf
upd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdfupd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdf
upd.18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23_FINAL.pdf
 
Її величність - українська книга презентація-огляд 2024.pptx
Її величність - українська книга презентація-огляд 2024.pptxЇї величність - українська книга презентація-огляд 2024.pptx
Її величність - українська книга презентація-огляд 2024.pptx
 
Відкрита лекція на тему «Біологічний захист рослин у теплицях»
Відкрита лекція на тему «Біологічний захист рослин у теплицях»Відкрита лекція на тему «Біологічний захист рослин у теплицях»
Відкрита лекція на тему «Біологічний захист рослин у теплицях»
 
Принципові відмінності досконалої (повної) конкуренції від інших форм організ...
Принципові відмінності досконалої (повної) конкуренції від інших форм організ...Принципові відмінності досконалої (повної) конкуренції від інших форм організ...
Принципові відмінності досконалої (повної) конкуренції від інших форм організ...
 
О.Духнович - пророк народної правди. Біографія
О.Духнович - пророк народної правди. БіографіяО.Духнович - пророк народної правди. Біографія
О.Духнович - пророк народної правди. Біографія
 
Бомбочки для ванни своїми руками презентація
Бомбочки для ванни своїми руками презентаціяБомбочки для ванни своїми руками презентація
Бомбочки для ванни своїми руками презентація
 
Віртуальна виставка нових надходжень 2-24.pptx
Віртуальна виставка нових надходжень 2-24.pptxВіртуальна виставка нових надходжень 2-24.pptx
Віртуальна виставка нових надходжень 2-24.pptx
 

Android Storage

  • 1. Лекція 6. Робота з даними на мобільних пристроях M.V.Davydov Lviv Polytechnic National University
  • 2. Storage options Shared Preferences Store private primitive data in key-value pairs. 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. Network Connection Store data on the web with your own network server. Use content providers to expose data to other aplications
  • 3. SharedPreferences SharedPreferences варто використовувати для зберігання примітивних даних: booleans, floats, ints, longs, and strings. Ці дані будуть збережені між користувацькими сесіями (навіть, якщо процес програми буде аварійно завершено).
  • 4. Отримання об’єкту SharedPreferences To get a SharedPreferences object for your application, use one of two methods: • getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. • getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
  • 5. Редагування SharedPreferences To write values: 1. Call edit() to get a SharedPreferences.Editor. 2. Add values with methods such as putBoolean() and putString(). 3. Commit the new values with commit()
  • 6. Приклад використання public class Calc extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); // We need an Editor object to make preference changes. // All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); } }
  • 7. SharedPreferences and UserPreferences Для автоматизації редагування і збереження користувацьких налаштувань зручніше використовувати PreferenceActivity. Shared preferences are not strictly for saving "user preferences," such as what ringtone a user has chosen. If you're interested in creating user preferences for your application, see PreferenceActivity, which provides an Activity framework for you to create user preferences, which will be automatically persisted (using shared preferences).
  • 8. Preference Activity example (from http:// alvinalexander.com/android/android-tutorial- preferencescreen-preferenceactivity-preferencefragment) 1 Define the UI in XML, using a PreferenceScreen as the main component/container 2 As an optional step, populate some Android string arrays to use with your preferences components 3 Write a PreferenceActivity and PreferenceFragment in Java code to show/handle the UI
  • 9. The res/xml/preferences.xml file <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/ res/android"> <EditTextPreference android:title="Your Name" android:key="username" android:summary="Введіть тут своє ім&apos;я"></EditTextPreference> <CheckBoxPreference android:title="Application Updates" android:defaultValue="false" android:summary="This option if selected will allow the application to check for latest versions." android:key="applicationUpdates" /> <ListPreference android:title="Download Details" android:summary="Select the kind of data that you would like to download" android:key="downloadType" android:defaultValue="1" android:entries="@array/listArray" android:entryValues="@array/listValues" /> </PreferenceScreen>
  • 10. The res/values/array.xml file <resources> <string-array name="listArray"> <item>Headings</item> <item>Headings and Details</item> <item>All Data</item> </string-array> <string-array name="listValues"> <item>1</item> <item>2</item> <item>3</item> </string-array> </resources>
  • 11. Activity code package com.alvinalexander.preferencestestapp; import android.os.Bundle; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; public class MyPreferencesActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit(); } public static class MyPreferenceFragment extends PreferenceFragment { @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } } }
  • 13. Java code to launch the PreferenceActivity Intent i = new Intent( this, MyPreferencesActivity.class); startActivity(i);
  • 14. Доступ до налаштувань з інших частин коду SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences( getBaseContext()); String strUserName = SP.getString("username", "NA"); boolean bAppUpdates = SP.getBoolean("applicationUpdates",false); String downloadType = SP.getString("downloadType","1");
  • 15. Переваги і недоліки налаштувань + дані можуть бути видимі або невидимі іншим програмам, в залежності від режиму доступу (якщо пристій"рутований", то ці дані можна прочитати з іншої програми) +зручно зберігати елементарні дані +підтримується атомарність внесення змін - не зручно використовувати для зберігання структурованих даних - можуть не видалятися при видалені програми на деяких пристроях або архівуватися, якщо в маніфесті програми не вказано <application … android:allowBackup="false">...</ application>
  • 16. Using the Internal Storage. Запис файлу у внутрішнє сховище You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed. To create and write a private file to the internal storage: 1. Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream. 2. Write to the file with write(). 3. Close the stream with close().
  • 17. Приклад запису файлу String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); MODE_PRIVATE створить файл (або замінить файл з тим самим ім’ям) і зробить файл приватним для вашої програми. Інші режими: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.
  • 18. Читання файлу 1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream. 2. Read bytes from the file with read(). 3. Then close the stream with close().
  • 19. Переваги і недоліки використання внутрішнього сховища + дані не видимі для читання іншим програмам (доки пристій не "рутований" (rooted) або явно не вказано MODE_WORLD_READABLE або MODE_WORLD_WRITEABLE) + можна зберігати дані у будь-якому форматі - користувачі завжди жаліються на нестачу внутрішньої пам’яті і просять її не використовувати для збереження великих файлів -контроль атомарності і цілісності цілком покладається на програміста
  • 20. Робота з тимчасовими файлами • If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files. • When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
  • 21. Корисні методи getFilesDir() Gets the absolute path to the filesystem directory where your internal files are saved. getDir() Creates (or opens an existing) directory within your internal storage space. deleteFile() Deletes a file saved on the internal storage. fileList() Returns an array of files currently saved by your application.
  • 22. Using the External Storage Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer. Caution: External storage can become unavailable if the user mounts the external storage on a computer or removes the media, and there's no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.
  • 23. Getting access to external storage In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions. For example: <manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... </manifest> If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well. Note: Beginning with Android 4.4, these permissions are not required if you are reading or writing only files that are private to your app. For more information, see the section below about saving files that are app-private.
  • 24. Checking media availability with getExternalStorageState()
  • 25. Checking media availability with getExternalStorageState() /* Checks if external storage is available for read and write */ public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } /* Checks if external storage is available to at least read */ public boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; }
  • 26. Saving files that can be shared with other apps Generally, new files that the user may acquire through your app should be saved to a "public" location on the device where other apps can access them and the user can easily copy them from the device. When doing so, you should use to one of the shared public directories, such as Music/, Pictures/, and Ringtones/. To get a File representing the appropriate public directory, call getExternalStoragePublicDirectory(), passing it the type of directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. By saving your files to the corresponding media-type directory, the system's media scanner can properly categorize your files in the system (for instance, ringtones appear in system settings as ringtones, not as music).
  • 27. Saving files that can be shared with other apps (приклад отримання папки для збереження зображень) public File getAlbumStorageDir(String albumName) { File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); if (!file.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } return file; }
  • 28. Необхідні дозволи у версії SDK<=18 Beginning with Android 4.4, reading or writing files in your app's private directories does not require the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions. So you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute: <manifest ...>     <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"            android:maxSdkVersion="18" />     ... </manifest>
  • 29. Saving files that are app-private If you are handling files that are not intended for other apps to use (such as graphic textures or sound effects used by only your app), you should use a private storage directory on the external storage by calling getExternalFilesDir(). This method also takes a type argument to specify the type of subdirectory (such as DIRECTORY_MOVIES). If you don't need a specific media directory, pass null to receive the root directory of your app's private directory. Ця папка буде видалена при деінсталяції програми, тому зберігати там особисті файли користувача не варто
  • 30. Важливо! • На деяких платформах getExternalFilesDir() може вертати папку з внутрішньої пам’яті. На цих платформах можна використовувати фугкцію getExternalFilesDirs() для отримання переліку папок для збереження даних (для Android <=4.3 ContextCompat.getExternalFilesDirs()) • Інші програми можуть отримати доступ до файлів до зовнішніх файлів програми, якщо вони мають право доступу READ_EXTERNAL_STORAGE
  • 31. Збереження тимчасових файлів на зовнішньому носії To open a File that represents the external storage directory where you should save cache files, call getExternalCacheDir(). If the user uninstalls your application, these files will be automatically deleted. You can also access a cache directory on a secondary external storage (if available) by calling ContextCompat.getExternalCacheDirs(). Tip: To preserve file space and maintain your app's performance, it's important that you carefully manage your cache files and remove those that aren't needed anymore throughout your app's lifecycle.
  • 32. Переваги і недоліки зовнішнього носія + зазвичай, має більше місця, ніж внутрішній носій + містить спеціальні папки для збереження файлів користувача (картинки, відео, музика, …) + програма може доступитися до всіх файлів зовнішнього носія з правом доступу READ_EXTERNAL_STORAGE - треба перевіряти наявність зовнішнього сховища - цілісність даних може бути порушена з різних причин - важче забезпечити приватність даних (треба шифрувати)
  • 33. Збереження даних у базі даних Android provides full support for SQLite databases. Бази даних не доступна для читання іншим програмам The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database. You can then get an instance of your SQLiteOpenHelper implementation using the constructor you've defined. To write to and read from the database, call getWritableDatabase() and getReadableDatabase(), respectively. These both return a SQLiteDatabase object that represents the database and provides methods for SQLite operations.
  • 34. Виконання запитів • You can execute SQLite queries using the SQLiteDatabase query() methods, which accept various query parameters, such as the table to query, the projection, selection, columns, grouping, and others. For complex queries, such as those that require column aliases, you should use SQLiteQueryBuilder, which provides several convienent methods for building queries. • Every SQLite query will return a Cursor that points to all the rows found by the query. The Cursor is always the mechanism with which you can navigate results from a database query and read rows and columns.
  • 35. Database debugging The Android SDK includes a sqlite3 database tool that allows you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases. See Examining sqlite3 databases from a remote shell to learn how to run this tool. adb -s emulator-5554 shell # sqlite3 /data/data/…/databases/rssitems.db SQLite version 3.3.12 Enter ".help" for instructions .... enter commands, then quit... sqlite> .exit
  • 36. Приклад використання бази даних package ua.lpnu.testshop; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.sql.SQLFeatureNotSupportedException; import java.util.Vector; import java.lang.String; /** * Created by maksymdavydov on 3/26/16. */
  • 37. public class LocalDatabase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 2; private static final String DATABASE_NAME = "myitems"; private static final String ITEM_TABLE = "items"; private static final String ITEM_NAME_COL = "item_name"; private static final String ITEMS_TABLE_CREATE_SQL = "CREATE TABLE "+ITEM_TABLE+" ( "+ITEM_NAME_COL+" TEXT )"; LocalDatabase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(ITEMS_TABLE_CREATE_SQL); for(int i=0;i<100;++i) { addItem("DatabaseItem" + i); } }
  • 38. @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO: implement when several database versions are available } public long addItem(String item_name) { ContentValues values = new ContentValues(); values.put(ITEM_NAME_COL, item_name); return getWritableDatabase().insert (ITEM_TABLE, null, values); }
  • 39. public Vector<String> getItems() { Vector<String> ret = new Vector<String>(); Cursor cursor = getReadableDatabase().rawQuery ("SELECT * FROM " + ITEM_TABLE, null); try { for(cursor.moveToFirst(); !cursor.isLast(); cursor.moveToNext()) { ret.add(new String(cursor.getString(0))); } } finally { cursor.close(); } return ret; } public void deleteItem(String item_name) { getWritableDatabase().delete(ITEM_TABLE, ITEM_NAME_COL + " = ?", new String[] {item_name}); } }
  • 40. Переваги і недоліки використання sqlite + структуровані дані, контроль за атомарністю операцій, транзакції + зручний набір функцій + вбудована підтримка версій - значно повільніше, ніж читати файл - виконання SQL запитів вимагає обережної роботи зі стрічками
  • 41. Зберігання даних в мережі (хмарі) + дані доступні з різних пристроїв + легше перевести користувача на підписку - можливі затримки при читанні, збереженні - складніше реалізувати - необхідно реалізувати кешування, синхронізацію, тощо Детальніше на наступній лекції