Mobile Computing With Android ACST 4550 Android Data Storage

Slides:



Advertisements
Similar presentations
Samsung Smart TV is a web-based application running on an application engine installed on digital TVs connected to the Internet.
Advertisements

XML, Uploading, Importing... Joshua Scotton.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
 data/data-storage.html#pref data/data-storage.html#pref 
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
MC365 XML Parsers. Today We Will Cover: An overview of the Java API’s used for XML processing Creating an XML document in Java Parsing an XML document.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
2. Setting Up Your Android Development Environment.
Android Development: File System Approaches Richard S. Stansbury 2015.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Data Persistence in Android
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Robert Fourer, Jun Ma, Kipp Martin Optimization Services Instance Language (OSiL), Solvers, and Modeling Languages Kipp Martin University of Chicago
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
SSC2: Web Services. Web Services Web Services offer interoperability using the web Web Services provide information on the operations they can perform.
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CIS 270—Application Development II Chapter 14—Files and Streams.
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
File Systems (1). Readings r Reading: Disks, disk scheduling (3.7 of textbook; “How Stuff Works”) r Reading: File System Implementation ( of textbook)
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
Persistent Storage using Perst Lite in Android. Introduction  Android by default uses SQLLite to handle data storage  SQLLite is a relational database.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Creating an Example Android App in Android Studio Activity lifecycle & UI Resources.
Object Oriented Software Development 10. Persistent Storage.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
User Interface Android GUI Tool OpenGL API XML Writer Optimized Layout Algorithm WPF Component Inter.
Operating Systems © 2007 Pearson Addison-Wesley. All rights reserved.
First Venture into the Android World Chapter 1 Part 2.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
XML Study-Session: Part III
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Chapter 11 File Systems and Directories. 2 File Systems (Chapter 11.1) File: 1. A named collection of related data. 2.smallest amount of information that.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
CHAPTER 9 File Storage Shared Preferences SQLite.
Week-9 (Lecture-1) XML DTD (Data Type Document): An XML document with correct syntax is called "Well Formed". An XML document validated against a DTD is.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Mobile Application Development Data Storage. Android provides several options for you to save persistent application data. The solution you choose depends.
Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.
Sequential files creation & writing
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Mobile Applications (Android Programming)
Android Content Providers & SQLite
Mobile Applications (Android Programming)
Data Storage: Part 2 (File System)
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Android SDK & App Development
Mobile Computing With Android ACST 4550 Android Animation
Android Storage.
Operation System Program 4
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Topics Introduction to File Input and Output
Mobile Computing With Android ACST 4550 Android Database Storage
Topics Introduction to File Input and Output
Preference Activity class
Presentation transcript:

Mobile Computing With Android ACST 4550 Android Data Storage

Working with Files and Directories Within the Android SDK, you can also find a variety of standard Java file utility classes (such as java.io) for handling different types of files, such as: Text files Binary files XML files Retrieving the file handle to a resource file is performed slightly differently from accessing files on the device file system, but once you have a file handle, your are allowed to perform read operations on the data in the standard way.

Working with Files and Directories (Cont’d) Android application file resources are part of the application package and are therefore accessible only to the application itself. What about file system files? Android application files are stored in a standard directory hierarchy on the Android file system.

Working with Files and Directories (Cont’d) Applications access the Android device file system using methods within the Context class: android.content.Context The application, or any Activity class, can use the application Context to access its private application file directory or cache directory. From here, you can add, remove, and access files associated with your application. By default, these files are private to the application and cannot be accessed by other applications or by the user.

Exploring with the Android Application Directories Android application data is stored on the Android file system in the following top-level directory: /data/data/<package name>/ Several default subdirectories are created for storing databases, preferences, and files as necessary. The actual location of these directories varies by device. You can also create other custom directories as needed. File operations all begin by interacting with the application Context object.

Exploring with the Android Application Directories (Cont’d) Method Purpose Context.deleteFile() Deletes a private application file by name Context.fileList() Gets a list of all files in the /files subdirectory Context.getCacheDir() Retrieves the application /cache subdirectory Context.getDir() Creates or retrieves a named application subdirectory Context.getExternalCacheDir() Retrieves the /cache subdirectory on the external file system (API Level 8) Context.getExternalFilesDir() Retrieves the /files subdirectory on the external file system (API Level 8) Context.getFilesDir() Retrieves the application /files subdirectory Context.getFileStreamPath() Returns the absolute file path to the application /files subdirectory Context.openFileInput() Opens a private application file for reading Context.openFileOutput() Opens a private application file for writing This table lists some important methods available for application file management. You can use all the standard java.io package utilities to work with FileStream objects.

Creating and Writing to Files in the Default Application Directory Android applications that require only the occasional file to be created should rely on the helpful Context class method called openFileOutput(). Use this method to create files in the default location under the application data directory: /data/data/<package name>/files/

Creating/Writing to Files in the Default Application Directory (Cont’d) The following code snippet creates and opens a file called Filename.txt. We write a single line of text to the file and then close the file: import java.io.FileOutputStream; ... FileOutputStream fos; String strFileContents = "Some text to write to the file."; fos = openFileOutput("Filename.txt", MODE_PRIVATE); fos.write(strFileContents.getBytes()); fos.close();

Creating and Writing to Files in the Default Application Directory (Cont’d) We can append data to the file by opening it with the mode set to MODE_APPEND: import java.io.FileOutputStream; ... FileOutputStream fos; String strFileContents = "More text to write to the file."; fos = openFileOutput("Filename.txt", MODE_APPEND); fos.write(strFileContents.getBytes()); fos.close();

Creating and Writing to Files in the Default Application Directory (Cont’d) The file we just created has the following path on the Android file system: /data/data/<package name>/files/Filename.txt

Reading from Files in the Default Application Directory Again, we have a shortcut for reading files stored in the default /files subdirectory. The following code snippet opens a file called Filename.txt for read operations: import java.io.FileInputStream; ... String strFileName = "Filename.txt"; FileInputStream fis = openFileInput(strFileName);

Reading Raw Files Byte by Byte You handle file-reading and file-writing operations using standard Java methods. The java.io.InputStreamReader and java.io.BufferedReader are used for reading bytes and characters from different types of primitive file types.

Reading Raw Files Byte by Byte (Cont’d) FileInputStream fis = openFileInput(filename); StringBuffer sBuffer = new StringBuffer(); BufferedReader dataIO = new BufferedReader (new InputStreamReader(fis)); String strLine = null; while ((strLine = dataIO.readLine()) != null) { sBuffer.append(strLine + "\n"); } dataIO.close(); fis.close(); Here is a simple example of how to read a text file, line by line, and store it in a StringBuffer.

Reading XML Files Package or Class Purpose android.sax.* Framework to write standard SAX handlers android.util.Xml XML utilities, including the XMLPullParser creator org.xml.sax.* Core SAX functionality (project: http://www.saxproject.org/) javax.xml.* SAX and limited DOM, Level 2 Core support org.w3c.dom Interfaces for DOM, Level 2 Core org.xmlpull.* XmlPullParser and XMLSerializer interfaces as well as a SAX2 Driver class (project: http://www.xmlpull.org/) The Android SDK includes several utilities for working with XML files, including SAX, an XML Pull Parser, and limited DOM, Level 2 Core support. This table lists the packages helpful for XML parsing on the Android platform.

Reading XML Files (Cont’d) <?xml version="1.0" encoding="UTF-8"?> <!-- Our pet list --> <pets> <pet type="Bunny" name="Bit"/> <pet type="Bunny" name="Nibble"/> <pet type="Bunny" name="Stack"/> <pet type="Bunny" name="Queue"/> <pet type="Bunny" name="Heap"/> <pet type="Bunny" name="Null"/> <pet type="Fish" name="Nigiri"/> <pet type="Fish" name="Sashimi II"/> <pet type="Lovebird" name="Kiwi"/> </pets> Your XML parsing implementation will depend on which parser you choose to use. Back in Chapter 6, we discussed including raw XML resource files in your application package. Here is a simple example of how to load an XML resource file and parse it using an XmlPullParser.

Reading XML Files (Cont’d) XmlResourceParser myPets = getResources().getXml(R.xml.my_pets); int eventType = -1; while (eventType != XmlResourceParser.END_DOCUMENT) { if(eventType == XmlResourceParser.START_DOCUMENT) { Log.d(DEBUG_TAG, "Document Start"); } else if(eventType == XmlResourceParser.START_TAG) { String strName = myPets.getName(); if(strName.equals("pet")) { Log.d(DEBUG_TAG, "Found a PET"); Log.d(DEBUG_TAG, "Name: "+ myPets.getAttributeValue(null, "name")); Log.d(DEBUG_TAG, "Species: "+ myPets.getAttributeValue(null, "type")); } eventType = myPets.next(); Log.d(DEBUG_TAG, "Document End"); The following code illustrates how to parse the preceding XML using a special pull parser designed for XML resource files.