CSCU9YH: Development with Android

Similar documents
Android: Data Storage

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

07. Data Storage

SQLite Database. References. Overview. Structured Databases

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

Data storage and exchange in Android

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

Database Development In Android Applications

Mobile and Ubiquitous Computing: Android Programming (part 4)

The Basis of Data. Steven R. Bagley

CS371m - Mobile Computing. Persistence

Mobile Programming Lecture 10. ContentProviders

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

Mobile Application Development Android

MOBILE APPLICATIONS PROGRAMMING

SharedPreferences Internal Storage External Storage SQLite databases

Mobile Application Development Android

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

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

An Android Studio SQLite Database Tutorial

SAVING SIMPLE APPLICATION DATA

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

Mobile Application Development Android

CS378 -Mobile Computing. Persistence

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

Atmiya Institute of Technology & Science, Rajkot Department of MCA

Spring Lecture 5 Lecturer: Omid Jafarinezhad

Android Programming Lecture 16 11/4/2011

Change in Orientation. Marco Ronchetti Università degli Studi di Trento

Mobile Computing Practice # 2d Android Applications Local DB

Android writing files to the external storage device

Data storage overview SQLite databases

Automatically persisted among application sessions

CS371m - Mobile Computing. Content Providers And Content Resolvers

Mediensystemen mit Android

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

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Learn about Android Content Providers and SQLite

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

ContentProvider & ContentResolver ContentResolver methods CursorLoader Implementing ContentProviders

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

CS Android. Vitaly Shmatikov

Files and IO, Streams. JAVA Standard Edition

Data Storage Methods in Android

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

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

Introduction to Programming Using Java (98-388)

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Starting Out with Java: From Control Structures Through Objects Sixth Edition

PIC 20A Streams and I/O

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

Mobile Application Programing: Android. Data Persistence

Software Engineering Large Practical: Preferences, storage, and testing

CS371m - Mobile Computing. Persistence - SQLite

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

IT101. File Input and Output

Programming with Android: Data management. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android File & Storage

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

Data Structures. 03 Streams & File I/O

CS378 -Mobile Computing. Persistence -SQLite

Persisting Data Making a Preference Screen

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

Object-Oriented Programming in the Java language

Android Development Tutorial

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Software Practice 1 - File I/O

EMBEDDED SYSTEMS PROGRAMMING SQLite

Sqlite Update Failed With Error Code 19 Android

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

SQL (and MySQL) Useful things I have learnt, borrowed and stolen

CS 251 Intermediate Programming Java I/O Streams

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

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

HST 952. Computing for Biomedical Scientists Lecture 8

Sri Vidya College of Engineering & Technology Question Bank

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Streams and File I/O

Lab 2: File Input and Output

Developing Android Applications

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Upcoming Assignments Quiz Friday? Lab 5 due today Alpha Version due Friday, February 26

Programming overview

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

Active Learning: Streams

Accelerating Information Technology Innovation

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

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Transcription:

: Development with Android Computing Science and Mathematics University of Stirling Data Storage and Exchange 1

Preferences: Data Storage Options a lightweight mechanism to store and retrieve keyvalue pairs of primitive data types File: storing data in files in device memory or in removable storage, e.g. an SD (Secure Data) card Database: Android supports SQLite Network: use the Internet to store and receive data, whether a relational database or just a text file 3 Preferences Use Context.getSharedPreferencesto read and write key-value pair Set as private to the app: MODE_PRIVATE Set as public: MODE_WORLD_READABLE SharedPreferences prefs = context.getsharedpreferences("details", MODE_PRIVATE); Editor edit = prefs.edit(); edit.clear(); edit.putstring("username", username ); edit.commit(); SharedPreferences prefs = context.getsharedpreferences("details", MODE_PRIVATE); String Uname = prefs.getstring("username", ""); 4 2

Reading/Writing Files Need to distiguish between files stored on internal storage (internal HDD) and externally (SD cards) For external storage WRITE_EXTERNAL_STORAGE is required <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.read_external_storage"/> Internal storage Always available. Files saved here are accessible by the creating app by default. External storage Not always availableas storage may be removed from the system. World-readable. Files saved here may be read/written/deleted outside of app. When the app is uninstalled, the system removes all its files from internal storage. Internal storage is best suited for private data files. When the app is uninstalled, the system removes files only if they were saved in the directory returned by getexternalfilesdir(). External storage is best for files which are intended to be publicly accessible including 5 accessible with a computer. Reading/Writing Files FileWriter f = new FileWriter("wrong.txt") not possible because of Android security system throws java.io.filenotfoundexception: /wrong.txt Each APK file installed gets a User ID from the OS: this ID is the key to the application s sandbox the sandbox protects against (malicious) other apps Files created by an app are signed with its ID: flags change access mode and make files accessible to other apps Flags can be combined using the OR operator (I) MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, MODE_PRIVATE, MODE_APPEND 6 3

File Writing - Internal Storage Write to a file with Context.openFileOutput: file name (with possible path and optional mode) returns a FileOutputStream FileOutputStream fileoutput = openfileoutput("sample.txt", Context.MODE_PRIVATE); OutputStreamWriter outputstream = new OutputStreamWriter(fileOutput); outputstream.write("hello Android"); // ensure that everything is really written out outputstream.flush(); outputstream.close(); fileoutput.close(); 7 File Writing External Storage Need to make sure external storage is available and writable: storagestate getexternalfilesdir() returns a location private to the app (however,read_external_storage permission allows to access all files on external storage, incl. data private to apps) Use getexternalstoragepublicdirectory() for public files String storagestate = Environment.getExternalStorageState(); if (storagestate.equals(environment.media_mounted)) { File file = new File(getExternalFilesDir(null), filename"); FileOutputStream fos = new FileOutputStream(file); OutputStreamWriter outputstream = new OutputStreamWriter(fos); outputstream.write("hello Android"); // ensure that everything is really written out outputstream.flush(); outputstream.close(); fileoutput.close(); 8 4

File Reading Internal Storage Reading a file with Context.openFileInput: file name (with possible path and optional mode) returns a FileInputStream emulator files are stored in: /data/package/files FileInputStream fileinput = openfileinput("sample.txt"); InputStreamReaderinputStream = new InputStreamReader(fileInput); char[] inputbuffer = new char[128]; inputstream.read(inputbuffer); String inputstring = new String(inputBuffer); inputstream.close(); fileinput.close(); 9 File Reading External Storage Again, need to check storage state Does not need to be writable String storagestate = Environment.getExternalStorageState(); if (storagestate.equals(environment.media_mounted) storagestate.equals(environment.media_mounted_read_only)) { File file = new File(getExternalFilesDir(null), filename"); FileInputStream fileinput = new FileInputStream(file); InputStreamReader inputstream = new InputStreamReader(fileInput); char[] inputbuffer = new char[128]; inputstream.read(inputbuffer); String inputstring = new String(inputBuffer); inputstream.close(); fileinput.close(); 10 5

Databases Android uses the built-in SQLite: open-source, stand-alone SQL database: widely used by popular applications/systems FireFoxuses SQLite to store configuration data iphone uses SQLite for database storage A database is private to the application: but data can be exposed through a content provider Emulator databases are stored in: /data/package/databases 11 Database Cursor The Cursor class is the return value for queries: Cursoris a pointer to the result set from a query this efficiently manage rows and columns A ContentValues object stores key-value pairs: putinserts keys with values of different data types 12 6

Database Example Create a helper class to encapsulate particular aspects of accessing the database: becomes transparent to the calling code create, open, close and use the database Book database example: id isbn title publisher 0 158603524X Using Android Wiley 1 0201101947 Android Decoded CRC Press 13 Database Helper class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DatabaseAdapter.DATABASE_NAME, null, DatabaseAdapter.DATABASE_VERSION); @Override public void oncreate(sqlitedatabase db) { db.execsql( "create table" + DatabaseAdapter.DATABASE_TABLE + "(" + "id integer primary key autoincrement," + "isbn textnotnull," + "title textnotnull," + "publisher text not null);" ); also overwrite onupgrade(), and optionally onopen() 14 7

Database Adapter public class DatabaseAdapter { public final static String KEY_ISBN = "isbn"; public final static String KEY_TITLE = title"; public final static String KEY_PUB = publisher"; public final static String DATABASE_NAME = "books"; public final static String DATABASE_TABLE = "titles"; public final static int DATABASE_VERSION = 1; private Context dbcontext; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DatabaseAdapter(Context context) { dbcontext = context; DBHelper = new DatabaseHelper(context); 15 Database Adapter: Inserting Data public DatabaseAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return(this); public void close() { DBHelper.close(); // insert new book, returning new row identifier public long insertbook(string isbn, String title, String publisher) { ContentValues contentvalues = new ContentValues(); contentvalues.put(key_isbn, isbn); contentvalues.put(key_title, title); contentvalues.put(key_pub, publisher); return(db.insert(database_table, null, contentvalues)); 16 8

Database Adapter: Retrieving Data // get a book, returning database pointer public Cursor getbook(long rowid) throws SQLException { //parameters to db.query include columns to return and condition //returned Cursor positioned before the first item, if not empty String where = id= + rowid; Cursor cursor = db.query(true, DATABASE_TABLE, where,...); return(cursor); 17 Database Adapter: Retrieving Data SQLiteDatabase::query(): table columns selection selectionargs groupby having orderby limit The table name to compile the query against. A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used. A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table. You may include?s in selection, which will be replaced by the values from selectionargs, in order that they appear in the selection. The values will be bound as Strings. A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped. A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used. How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause. 18 9

Database Adapter: Deleting/Updating Data // delete a book, use WHERE argument with rowid //returning true if it worked public boolean deletebook(long rowid) { String where = id= + rowid; return(db.delete(database_table, where,...) > 0); // update a book, returning true if it worked public boolean updatebook(long rowid, String isbn, String title, String publisher) { String where = id= + rowid; ContentValues contentvalues = new ContentValues(); contentvalues.put(key_isbn, isbn);... return(db.update(database_table, contentvalues, where,...) > 0); 19 Using The Database Instantiate DatabaseAdapter in the Activity constructor public class MyActivity extends Activity { DatabaseAdapter db; @Override publicvoid oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); db = newdatabaseadapter(this); db.open(); db.insertbook( 1234567, Brilliant Book, Great Publisher ); Cursor cursor = db.getbook(0); cursor.movetofirst(); while (!cursor.isafterlast()) { String isbn = cursor.getstring(0); String title = cursor.getstring(1); cursor.movetonext(); 20 10

Content Providers Files and databases are normally private: unless specifically created otherwise Content providers provide data to other apps: retrieve, modify and create data Example content providers in Android are Contacts and MediaStore(audio, images, video) Data is mapped to a URI used by clients: content://contacts/people/ all contact names content://contacts/people/23 contact with ID 23 Uri.parse("content://contacts/people/23") managedquery(myperson, ) 21 Customised Content Provider Extend android.content.contentprovider Required methods need overriding: oncreate() called when a provider is created gettype(uri) return MIME type of data insert(uri, contentvalues) query(uri, columns, selection, selectionargs, sortorder) update(uri, contentvalues, selection, selectionargs) delete(uri, selection, selectionargs) To create a CP for the earlier DB example, these methods can call the methods in the DatabaseAdapter class. lab session 22 11