cosc 4730 files: bb: file connection optional package android: internal and external storage

42
Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Post on 20-Dec-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Cosc 4730

Files:BB: File Connection Optional PackageAndroid: Internal and External storage

Page 2: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Note about Streams.

• In Java SE– We use BufferedWriter and BufferedReader streams

for most io– These are not available

• Generally we get InputStream and OutputStream– And pass them to DataInputStream and

DataOutputStream– On the BB we can also use the InputStreamReader

and outputStreamWriter

Page 3: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

DataOutputStream• DyteArrayOutputStream baos = new ByteArrayOutputStream();• DataOutputStream dos = new DataOutputStream(baos);

– New you write records data to dos use one or more methods• write: writes an array of bytes, with an offset and length of byte array• writeBoolean: writes a boolean to the stream• writeByte: writes one byte to the stream• writeChar: writes one character to the stream• writeChars: write a String as a sequence of characters• writeDouble: converts Double to long (doubleToLongBits) and writes long to stream• writeFloat: converts float to a int (floatToIntBits) and writes the int to the stream• writeInt: writes an integer to the stream• writeLong: writes a long to the stream• writeShort: writes a short intger to the stream• writeUTF: writes a String using UTF-8 encoding to the stream

• Once that's done, use baos variable to create the byte variable– byte[] bytes = boas.toByteArray();

Page 4: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

DataInputStream• When reading back the data:

– byteArrayInputStream bais = new ByteArrayInputStream(bytes); • where bytes is the array of bytes read from the RecordStore

– DataInputStream dis = new DataINputStream(bais);– Now read it back the same way it written, using:

• read: Reads an array of bytes from the stream• readBoolean: Reads a boolean from the stream• readByte: Reads a single byte from the stream• readChar: Reads a character from the stream• readDouble: Reads a long from the stream and converts it to a double• readFloat: reads an int from the stream and converts it to a float• readInt: reads an integer from the stream• readLong: reads long integer from the stream• readShort: read a short integer from the stream• readUTF: reads a string in UTF-8 enconding.

Page 5: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

BLACKBERRY

Page 6: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

File Connection Optional Package

• fcop as the name states is an optional package, which may not be on be on every phone.– Does the phone even have a "file system". If not,

then fcop won't be there either.• Most phones that take memory cards, should have fcop

though.

– A note, fcop is also bundled with the PIM optional packages as well.

Page 7: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

How it connects to the O/S

• The connector class creates the instances of the FileConnection interface to manage the access to the files on the device's file system.– Connector gives you

access to input and output streams as well.

Page 8: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Is FCOP present?

• If you are writing for multiple phones, you may have figure out if it exists on that phone at runtime.– Some simple code to know if it exists or not. String cv = System.getProperty("microedition.io.file.FileConnection.version"); if (cv != null) { System.out.println("FCOP is present"); System.out.println("Version is "+cv); //yes, FCOP is present. cv may say

"1.0", but any non null says it exists} else { //no FCOP is present, use RecordStore?}

Page 9: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Root file system.• "file:///" required to access the file system.

• NOTE: Three forward slashes are required.

– But after that, it phone specific.• Such as names of media cards• "file:///MediaCard/", "file:///root1/", etc.

• Get some default directories– String srcdir = System.getProperty("fileconn.dir.photos");

• fileconn.dir.photos: Image capture through your Mobile camera.• fileconn.dir.videos: Video capture through your Mobile camera. • fileconn.dir.tones: Ring tones default directory.• fileconn.dir.memorycard: Memory Card / SD Card / Flash Drive root directory • see http://

www.blackberry.com/developers/docs/5.0.0api/java/lang/System.html PDAP section for a complete list.

Page 10: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Finding root file systems• FileSystemRegistry.listRoots()

– returns an Enumeration of root the file Systems• Example to list each the root filesystem Enumeration drives = FileSystemRegistry.listRoots();while (drives.hasMoreElements()) { String driveString = drives.nextElement().toString(); System.out.println(driveString + "\n"); } }Output:system/SDCard/• Note, to access these, you need to add file:/// to them

– file:///SDCard/

Page 11: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Opening a Filesystem

• Uses the Connector to make a access the filesystem.String fs = "file:/// SDCard /"; //assuming SDCard / is a

filesystem.FileConnection fc = (FileConnection) Connector.open(fs);

• Now we use fc and the methods in FileConnection

Page 12: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

FileConnection directory methods

• long availableSize()– Free memory on available on the file system for (files or

directories).• boolean canRead() – Checks if the file or directory is readable.

• boolean canWrite()– Checks if the file or directory is writable.

• long directorySize(boolean includeSubDirs) – Determines the size in bytes on a file system of all of

the files that are contained in a directory.

Page 13: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

FileConnection directory methods (2)

• Enumeration list()– Gets a list of all visible files and directories

contained in a directory. • Enumeration list(String filter,

boolean includeHidden)– Gets a filtered list of files and directories

contained in a directory.– Note, filter string, can use wildcard *• To see everything including hidden list("*", true);

Page 14: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Directory list example

try { FileConnection fc = (FileConnection)

Connector.open("file:///SDCard/"); Enumeration files = fc.list(); while (files.hasMoreElements()) { System.out.println((String)files.nextElement()); }} catch (IOException ex) { //failed to open }

Page 15: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

FileConnection directory methods (3)

• void mkdir()– Creates a directory corresponding to the directory string provided in the Connector.open() method.– Example

• fc = (FileConnection) Connector.open("file://root1/mydir"); • fc.mkdir();

• void setReadable(boolean readable)– Sets the file or directory readable attribute to the indicated value.

• void setWritable(boolean writable)– Sets the selected file or directory writable attribute to the indicated value.

• void rename(String newName)– Renames the selected file or directory to a new name in the same directory.

• boolean exists()– Checks if the file or directory specified in the URL passed to the Connector.open() method exists.

• boolean isDirectory()– Checks if the URL passed to the Connector.open() is a directory.

• void close()– Closes the connection

Page 16: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

File I/O

• we can use the following streams– DataInputStream openDataInputStream()• Open and return a data input stream for a connection.

– DataOutputStream openDataOutputStream()• Open and return a data output stream for a connection.

– InputStream openInputStream()• Open and return an input stream for a connection.

– OutputStream openOutputStream()• Open and return an output stream for a connection.

Page 17: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

FileConnection File methods• Look for some in the Directory methods.• void create()

– Creates a file corresponding to the file string provided in the Connector.open() method for this FileConnection

• long fileSize()– Determines the size of a file on the file system.

• boolean isHidden()– Checks if the file is hidden.

• boolean isOpen()– Returns an indication of whether the file connection is currently open or

not.• void setFileConnection(String fileName)

– Resets this FileConnection object to another file or directory.

Page 18: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Open a file.

• Uses same connector.open()– but need to specify one more parameter.• Connector.READ• Connector.READ_WRITE• Connector.WRITE

Page 19: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

File Exampleimport javax.microedition.io.*;import javax.microedition.io.file.*;import java.io.*;import java.util.*;• First let's create the file and write something to it.String fs = "file:///SDCard/myfile.txt";FileConnection fc = (FileConnection) Connector.open(fs,Connector.WRITE);fc.create();DataOutputStream dos = fc.openDataOutputStream();dos.writeUTF("Hi there.");dos.close(); //close everything and return the resources.fc.close();• Now read back the information.fc= (FileConnection) Connector.open(fs,Connector.READ);DataInputStream dis = fc.openDataInputStream();String t = dis.readUTF();dis.close();fc.close();System.out.println("read from file: "+t);

Page 20: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

The other 2 streams

• If you want to read/write text instead of bytes, then you can use the OpenInputStream and OpenOutputStream

• And use the InputStreamReader and outputStreamWriter• output example: String t = "Hi There"; OutputStreamWriter out = new

OutputStreamWriter(fc.openOutputStream());out.write(t);out.close();fc.close();

Page 21: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

The other 2 streams (2)• input is a little harder, since InputStreamReader read() returns one

character at a time and it's an integer value fc= (FileConnection) Connector.open(fs,Connector.READ);InputStreamReader in = new InputStreamReader(fc.openInputStream());t = "";//now read in the text file, one character at a time.int i = in.read();while (i !=-1) { //To stop at end of line use (i !=-1 && (char) i != '\n')

t += (char)i;i = in.read();

}fc.close();System.out.println("read from file: "+t);

Page 22: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

The other 2 streams (2)

• Using these can make things simpler.• But– Now you have split up the strings• String tokenizers covered at the end of this lecture.

– Convert numbers to the correct type.• Otherwise– Now you can read text files created on most

machines and write files that can be read by most computers as well.

Page 23: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

PrintStream

• You may also find the printStream useful as well for output.– one doesn't throw exceptions, sets a status flag– Uses print and println– Example

String t = "Hi There"; int x = 12; PrintStream out = new

PrintStream(fc.openOutputStream());out.println(t);out.print(x); out.print(" "); out.println(x);

Page 24: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

FileSystemListener

• There is a listener, to see if the root file systems have changed (removed/added)

• You override the default rootChanged (int state, String rootName) method– state is based on ROOT_ADDED or

ROOT_REMOVED – rootName is the name of the changed root File

System

Page 25: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Lastly

• Apps can do a lot of damage with direct access to the file system.– vendors can/do two things:• Hide sections of the file system from third party

applications.• Require third party applications to be signed, before

they can access the file system.– but not needed for the emulators.

• Blackberry only requires a signature for DRM file accessor methods.

Page 26: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Static files in the COD

• You can read files out of the COD file as well.• Uses the Class method to find cod on the

filesystem.– Returns a InputStream

Class classs = Class.forName("edu.cs4755.fileSystem");//to actually retrieve the resource prefix the name of the file with a "/"InputStream is = classs.getResourceAsStream(“/file.txt”);

Page 27: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

ANDROID

Page 28: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Android and data Storage

• There are several methods to store data– Preferences • is a lightweight mechanism to store and retrieve key-

value pairs of primitive data types. It is typically used to store application preferences

– Content Providers• Only method to "share" information between

applications.

– Files System– Databases

Page 29: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Internal vs External storage

• 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.• External storage

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

Page 30: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Internal Storage

• FileOutputStream openFileOutput(String File, MODE)– File: the filename for output– MODE: Context.MODE_PRIVATE, MODE_APPEND,

MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.• Note If the file already exists, then it will be overwritten, except

with MODE_APPEND.

• It returns a FileOutputStream, which we then send to DataOutputStream.

• Close() when you are finished with the file.

Page 31: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Internal Storage (2)• FileInputStream openFileInput(String File)

– Open the files named.– Again, send it to DataInputStream for ease of reading.

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

• String[] fileList()– Returns an array of files currently saved by your application.

Page 32: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Internal Example• Outputdos = new DataOutputStream( openFileOutput("FileExample", Context.MODE_PRIVATE));dos.writeUTF("First line of the file");dos.writeUTF(“Second line of the file");dos.close();

• InputString flist[] = fileList();in = new DataInputStream( openFileInput(flist[0]) ) ;while(true)

try { label1.append(in.readUTF() + "\n"); //label1 is a TextView} catch (EOFException e) { //reach end of file in.close();}

Page 33: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Files in the apk

• If you have a static (read only) file to package with your application at compile time, you can save the file in your project in res/raw/myDataFile, and then open it with getResources().openRawResource (R.raw.myDataFile). – It returns an InputStream object that you can use

to read from the file.

Page 34: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

External files• Before accessing files on external storage, you’ll need to check to see if the media is available.• Code to checkboolean mExternalStorageAvailable = false;boolean mExternalStorageWriteable = false;String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true;} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false;} else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false;}

Page 35: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

External Files (2)

• //API 8 and above– getExternalFilesDir() • For none shared files• Creates the directory

/Android/data/<package_name>/files/ • Directory and all files deleted when app is uninstalled.

– getExternalStoragePublicDirectory()• For files that are to be shared.• Not deleted when app is uninstalled.

Page 36: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Environment. directoriesDIRECTORY_ALARMS Standard directory in which to place any audio files that should be in the list of alarms

that the user can select (not as regular music).

DIRECTORY_DCIM The traditional location for pictures and videos when mounting the device as a camera.

DIRECTORY_DOWNLOADS Standard directory in which to place files that have been downloaded by the user.

DIRECTORY_MOVIES Standard directory in which to place movies that are available to the user.

DIRECTORY_MUSIC Standard directory in which to place any audio files that should be in the regular list of music for the user.

DIRECTORY_NOTIFICATIONS

Standard directory in which to place any audio files that should be in the list of notifications that the user can select (not as regular music).

DIRECTORY_PICTURES Standard directory in which to place pictures that are available to the user.

DIRECTORY_PODCASTS

Standard directory in which to place any audio files that should be in the list of podcasts that the user can select (not as regular music).

DIRECTORY_RINGTONES

Standard directory in which to place any audio files that should be in the list of ringtones that the user can select (not as regular music).

Page 37: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

External Files (3)

• File file = new File(getExternalFilesDir(null), “myfile.txt");– Create directory if needed and the spot for the

file, called myfile.txt– null: it’s a file for my application– If I wanted a music directory then• File file = new

File(getExternalFilesDir( Environment.DIRECTORY_MUSIC), “file.mp3");• Place in the a subdirectory of music.

Page 38: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

External Files (4)• Read

– in = new DataInputStream(new FileInputStream(file) );• Write

• dos = new DataOutputStream(new FileOutputStream(file) );• Append:

– dos = new DataOutputStream(new FileOutputStream(file),true );

• Read or write as normal. Then close the file when you are done.• Other useful methods of file

– file.exists() //return true if file exists– file.delete(); //deletes the file– See http://developer.android.com/reference/java/io/File.html for more.

Page 39: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

external files (5)

• getExternalStoragePublicDirectory() File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES);• There is no NULL version. File file = new File(path, "DemoPicture.jpg");// Make sure the directory exists.path.mkdirs();//Now use file just like before.

Page 40: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

STRINGSAdditional notes.

Page 41: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

Breaking up strings.

• Blackberry• Note, neither split nor StringTokenizer are not included in JavaME,

so Blackberry does not include it either.• There is a very simple “split” in the StringUtilities package, call

stringToWords and stringToKeywords which splits on non-characters.

• For full split function see the following doc. It is also included in the blackberry filesystem example.

• Android• the string.split(String RegularExpression) is included on Android.

So String[] = line.split(“ “) would break up the string in line based on a space and put the resulting string array

• Java.util has the StringTokenizer, but considered “legacy”.

Page 42: Cosc 4730 Files: BB: File Connection Optional Package Android: Internal and External storage

QA&