Android: Data Storage

Similar documents
CS378 -Mobile Computing. Persistence

CS371m - Mobile Computing. Persistence

MOBILE APPLICATIONS PROGRAMMING

Eng. Jaffer M. El-Agha Android Programing Discussion Islamic University of Gaza. Data persistence

07. Data Storage

Mobile and Ubiquitous Computing: Android Programming (part 4)

CSCU9YH: Development with Android

The Basis of Data. Steven R. Bagley

SQLite Database. References. Overview. Structured Databases

Spring Lecture 5 Lecturer: Omid Jafarinezhad

How-to s and presentations. Be prepared to demo them in class. ons

SharedPreference. <map> <int name="access_count" value="3" /> </map>

SharedPreferences Internal Storage External Storage SQLite databases

Mobile Application Development Android

How to access your database from the development environment. Marco Ronchetti Università degli Studi di Trento

Data storage and exchange in Android

Single Application Persistent Data Storage. CS 282 Principles of Operating Systems II Systems Programming for Android

Database Development In Android Applications

Mobile Application Development Android

SQLite. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 6: Working with Databases. What is a Database Server. Advantages of SQLite

Mobile Application Development Android

Mobile Computing Practice # 2d Android Applications Local DB

Mobile Application Programing: Android. Data Persistence

SAVING SIMPLE APPLICATION DATA

Mr. Pritesh N. Patel Assistant Professor MCA ISTAR, V. V. Nagar ANDROID DATABASE TUTORIAL

Android Programming Lecture 16 11/4/2011

An Android Studio SQLite Database Tutorial

Android File & Storage

Android Database Programming

Atmiya Institute of Technology & Science, Rajkot Department of MCA

Automatically persisted among application sessions

Android Components. Android Smartphone Programming. Matthias Keil. University of Freiburg

Mobile Programming Lecture 10. ContentProviders

Android Components Android Smartphone Programming. Outline University of Freiburg. Data Storage Database University of Freiburg. Notizen.

How to access your database from the development environment. Marco Ronchetti Università degli Studi di Trento

Mediensystemen mit Android

Object-Oriented Databases Object-Relational Mappings and Frameworks. Alexandre de Spindler Department of Computer Science

Content Provider. Introduction 01/03/2016. Session objectives. Content providers. Android programming course. Introduction. Built-in Content Provider

Basic UI elements: Defining Activity UI in the code. Marco Ronchetti Università degli Studi di Trento

Managing Data. However, we'll be looking at two other forms of persistence today: (shared) preferences, and databases.

Persisting Data Making a Preference Screen

Data Storage Methods in Android

EMBEDDED SYSTEMS PROGRAMMING SQLite

MyDatabaseHelper. public static final String TABLE_NAME = "tbl_bio";

Data storage overview SQLite databases

CS371m - Mobile Computing. Persistence - SQLite

Data Persistence. Chapter 10

Software Engineering Large Practical: Preferences, storage, and testing

Required Core Java for Android application development

Android Application Development Course 28 Contact Hours

LẬP TRÌNH DI ĐỘNG. Bài 9: Lưu Trữ & SQLite

Introduction to Programming Using Java (98-388)

Core Java Contents. Duration: 25 Hours (1 Month)

JAVA. Duration: 2 Months

Lab 5 Periodic Task Scheduling

Files and IO, Streams. JAVA Standard Edition

Android Camera. Alexander Nelson October 6, University of Arkansas - Department of Computer Science and Computer Engineering

Debojyoti Jana (Roll ) Rajrupa Ghosh (Roll ) Sreya Sengupta (Roll )

CS371m - Mobile Computing. Content Providers And Content Resolvers

Programming in Android. Nick Bopp

App Development for Smart Devices. Lec #5: Content Provider

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

CS378 -Mobile Computing. Persistence -SQLite

Android Application Development

CS Android. Vitaly Shmatikov

Andorid Storage Options

Writing Efficient Drive Apps for Android. Claudio Cherubino / Alain Vongsouvanh Google Drive Developer Relations

How to access your database from the development environment. Marco Ronchetti Università degli Studi di Trento

Lecture 2 Android SDK


CS 4518 Mobile and Ubiquitous Computing Lecture 6: Databases, Camera, Face Detection Emmanuel Agu

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

PIC 20A Streams and I/O

Chapter 10. IO Streams

DEVELOPMENT OF PUBLIC FACILITY AND HOUSEHOLD LOCATOR TOOL USING MOBILE GIS AND ANDROID TECHNOLOGY

File Operations in Java. File handling in java enables to read data from and write data to files

Data Structures. 03 Streams & File I/O

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

JAVA. 1. Introduction to JAVA

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Complete Java Contents

Android Application Model II. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Software Engineering Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 27, 2017

CMSC436: Fall 2013 Week 4 Lab

In-app Billing Version 3

PES INSTITUTE OF TECHNOLOGY (BSC) IV MCA, Second IA Test, May 2017 Mobile Applications (13MCA456) Solution Set Faculty: Jeny Jijo

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Firebase Realtime Database. Landon Cox April 6, 2017

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Active Learning: Streams

Android. The Toolbar

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CN208 Introduction to Computer Programming

Understanding Application

Android Programming - Jelly Bean

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Java 5 New Language Features

Transcription:

Android: Data Storage F. Mallet Frederic.Mallet@unice.fr Université Nice Sophia Antipolis

Outline Data Storage Shared Preferences Internal Storage External Storage SQLite Databases Network Connection F. Mallet http://developer.android.com/guide/topics/data/data-storage.html 2

Shared Preferences SharedPreferences gives a framework To save and retrieve persistent key-value pairs Only for primitive data types: boolean, floats, ints, longs, strings Will persist across sessions even if the application is killed API getpreferences(int mode) Only one preference file for one activity MODE_PRIVATE: all the applications sharing the same user ID MODE_WORLD_READABLE: deprecated in API 17 => ContentProvider MODE_WORLD_WRITABLE: deprecated in API 17 => ContentProvider getsharedpreferences(string name, int mode) Multiple preferences files for one activity or shared among activities F. Mallet 3 http://developer.android.com/reference/android/content/sharedpreferences.html

Shared Preferences SharedPreferences gives a framework To save and retrieve persistent key-value pairs Only for primitive data types: boolean, floats, ints, longs, strings Will persist across sessions even if the application is killed API getpreferences(int mode) Calls getsharedpreferences( application name, mode); getsharedpreferences(string name, int mode) Multiple preferences files for one activity or shared among activities If it does not exist, will be created F. Mallet 4

Reading values from a key boolean contains(string key) Map<String,?> getall() boolean getboolean(string key, boolean defvalue) float getfloat(string key, float defvalue) int getint(string key, int defvalue) long getlong(string key, long defvalue) String getstring(string key, String defvalue) Writing values SharedPreferences.Editor edit() Atomic changes to be committed SharedPreferences F. Mallet 5

Atomic changes to preferences void apply() boolean commit() SharedPreferences.Editor SharedPreferences.Editor clear() SharedPreferences.Editor remove(string key) SharedPreferences.Editor putboolean(string key, boolean value) SharedPreferences.Editor putfloat(string key, float value) SharedPreferences.Editor putint(string key, int value) SharedPreferences.Editor putlong(string key, long value) SharedPreferences.Editor putstring(string key, String value) F. Mallet 6

Internal Storage Open or create a file private to an application Removed when the application is uninstalled Class android.content.context FileOutputStream openfileoutput (String name, int mode) MODE_PRIVATE (Default) MODE_APPEND MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE FileInputStream openfileinput (String name) String[] filelist () boolean deletefile (String name) File getfilesdir () File getdir (String name, int mode) (creates if does not exist) File getcachedir () F. Mallet 7

Same as standard JDK Use BufferedInputStream with FileInputStream Use FileReader for flows of characters Use BufferedOutputStream with FileOutputStream Use FileWriter for flows of characters Use File: (mkdirs(), exists(), ) Do not forget to close the streams Example: String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openfileoutput(filename, Context.MODE_PRIVATE); fos.write(string.getbytes()); fos.close(); Package java.io F. Mallet 8

External Storage Removable storage media (SD card) or internal storage Data are world_readable Accessible through USB mass storage Need permissions From Android 4.4 (API 18) Permissions are not required to access private directories Read only <manifest...> <uses-permission android:name="android.permission.read_external_storage" />... </manifest> Write (and read) <manifest...> <uses-permission android:name="android.permission.write_external_storage" />... </manifest> F. Mallet 9

Checking media availability Check Read Access public boolean isexternalstoragereadable() { String state = Environment.getExternalStorageState(); return (Environment.MEDIA_MOUNTED.equals(state) Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)); Check Write Access public boolean isexternalstoragewritable() { String state = Environment.getExternalStorageState(); return (Environment.MEDIA_MOUNTED.equals(state); F. Mallet 10

class android.content.environment Public shared directory: File getexternalstoragepublicdirectory (String type) Private external directory: File getexternalfilesdir (String type) Types External Directories String DIRECTORY_ALARMS Standard directory for any audio files that should be in the list of alarms that the user can select (not as regular music). String DIRECTORY_DCIM Pictures and videos when mounting the device as a camera. String DIRECTORY_DOCUMENTS Documents created by the user. String DIRECTORY_DOWNLOADS files downloaded by the user. F. Mallet 11

More Types External Directories String DIRECTORY_MOVIES movies available to the user. String DIRECTORY_MUSIC Any audio files in the regular list of music for the user. String DIRECTORY_NOTIFICATIONS Any audio files in the list of notifications that the user can select (not as regular music). String DIRECTORY_PICTURES Pictures available to the user. String DIRECTORY_PODCASTS Qny audio files in the list of podcasts that the user can select (not as regular music). String DIRECTORY_RINGTONES Any audio files in the list of ringtones that the user can select (not as regular music). F. Mallet 12

Using DataBases Extend an helper class: SQLiteOpenHelper Use its oncreate method to create tables Call SQLiteDatabase getwritabledatabase() to get write access Call SQLiteDatabase getreadabledatabase() to get read access Use the query() method of SQLiteDatabase Example: public class DictionaryOpenHelper extends SQLiteOpenHelper { private static final String DICTIONARY_TABLE_NAME = "dictionary"; private static final String DICTIONARY_TABLE_CREATE = "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" + KEY_WORD + " TEXT, " + KEY_DEFINITION + " TEXT);"; DictionaryOpenHelper(Context context) { super(context, DATABASE_NAME, null, 1 /* version */); @Override public void oncreate(sqlitedatabase db) { db.execsql(dictionary_table_create); F. Mallet http://www.sqlite.org/lang_createtable.html 13

Schema and Contract Schema: declaration of how the database is organized Contract class: (recommended) Companion class to reflect the schema (CONSTANTS) Global definitions, Inner classes for each table Inherit from BaseColums to get an _ID (used by Cursor) Example: public final class ExampleContract { public static final String DATABASE_NAME = exampledb"; private ExampleContract() { /* NOBODY CAN INSTANTIATE */ public static abstract class ExampleEntry implements BaseColumns { public static final String TABLE_NAME = mytable"; public static final String COLUMN_NAME_CATEGORY = category"; public static final String COLUMN_NAME_WORD = word"; public static final String SQL_CREATE = "CREATE TABLE " + TABLE_NAME + "( " + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME_CATEGORY + " TEXT, " + COLUMN_NAME_WORD + " TEXT);"; F. Mallet.. 14

Using DataBases Extend an helper class: SQLiteOpenHelper Example: public class DictionaryOpenHelper extends SQLiteOpenHelper { DictionaryOpenHelper(Context context) { super(context, ExampleContract.DATABASE_NAME, null, 1 /* version */); @Override public void oncreate(sqlitedatabase db) { db.execsql(examplecontract.exampleentry.sql_create); @Override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql(tablecontract.wordcontract.sql_delete); oncreate(db); @Override public void ondowngrade(sqlitedatabase db, int oldversion, int newversion) { onupgrade(db, oldversion, newversion); F. Mallet 15

Method insert of SQliteDatabase Use ContentValues Example SQLiteDatabase db = helper.getwritabledatabase(); Adding a row in a table ContentValues values = new ContentValues(); values.put(examplecontract.exampleentry.column_name_category,category); values.put(examplecontract.exampleentry.column_name_word, word); long newrowid = db.insert(examplecontract.exampleentry.table_name, null, // name of columns that can be null values); F. Mallet 16

Deleting a row Method insert of SQliteDatabase Make a SQLite query for deletion Example (in Contract) public static final String DELETE_ROW = COLUMN_NAME_WORD + " =?"; Example (to delete the row) SQLiteDatabase db = helper.getwritabledatabase(); int nb = db.delete(examplecontract.exampleentry.table_name, ExampleContract.ExampleEntry.DELETE_ROW, new String[]{word); F. Mallet 17

Selecting rows Use Query method in SQLiteDatabase public Cursor query (String table, String[] columns, String selection, String[] selectionargs, String groupby, String having, String orderby) Use the Cursor to iterate through the result Example SQLiteDatabase db = helper.getreadabledatabase(); Cursor c = db.query(examplecontract.exampleentry.table_name, null, null, null, null, null, ExampleContract.ExampleEntry.COLUMN_NAME_CATEGORY); List<DBLine> mylist = new ArrayList<>(); while (c.movetonext()) { mylist.add(new DBLine(c.getString(1), c.getstring(2))); F. Mallet 18

References Android Developers http://developer.android.com/training/index.html Other lectures at UNS P. Renevier E. Amosse Books Beginning Android, M. L. Murphy, APress Android Programming: The Big Nerd Ranch Guide, B. Phillips, B. Hardy, http://www.bignerdranch.com F. Mallet 19