A strategy that allows the reusing of volatile objects and other data items by storing them into a permanent storage system such as disk files and databases
File IO management in Android
Streams
Scanner
PrintWriter
Permanent file storage locations in Android
Internal main memory
External SD card
Internal files
Also called Resource Files or Embedded Files, they share space with other application's resources such as code, icons, pictures, music, etc.
Factors determining permanent data storage destination
Size (small/large)
Location (internal/external)
Accessibility (private/public)
Persistent environment options
Store private primitive data in key-value pairs
Store private data on the device's main memory
Store public data on the shared external storage
Store structured data in a private/public database
Store data on the web
Shared Preferences
Good for handling a handful of items, data is saved as <Key, Value> pairs where the key is a string and the value must be a primitive data type, they are permanent unlike Java Maps
Shared Preferences usage
Typically used to keep state information and shared data among several activities of an application
Using Shared Preferences API
1. Get Shared Preferences object
2. Get Shared Preferences Editor object
3. Put data using putXxx methods
4. Commit changes
5. Retrieve data using getXxx methods
Shared Preference containers are saved as XML files in the application's internal memory space
Android applications may include resource elements such as those in res/drawable, res/raw, res/menu, res/style
Reading an internal resource file
1. Get resource ID
2. Open input stream on resource
3. Create BufferedReader to read lines
4. Append lines to string buffer
5. Close reader and stream
6. Display text in TextView
Android requires resource file names to be in lowercase form
If the resource file is not found, it raises a NotFoundException condition
Conventional IO housekeeping operations should be issued to close the reader and stream objects
Raw file
An arbitrary dataset stored in its original raw format (such as .docx, pdf, gif, jpeg, etc)
Accessing raw files
1. Through an InputStream acting on a R.raw.filename resource entity
2. Android requires resource file names to be in lowercase form
3. getResources().openRawResource(fileResourceId) creates an InputStream object that sends the bytes from the selected resource file to an input buffer
4. If the resource file is not found it raises a NotFoundException condition
Extracting lines from input buffer
A BufferedReader object is responsible for extracting lines from the input buffer and assembling a string which finally will be shown to the user in a textbox
Protocol expects that conventional IO house keeping operations should be issued to close the reader and stream objects
Internal resource file
Private and cannot be seen by other apps residing in main memory
The internal resource file (notes.txt) is stored in the phone's internal memory under the name: /data/data/cis470.matos.fileresources/files/notes.txt
openFileInput(FILE_NAME)
Opens a private file linked to this Context's application package for reading
Reading from internal file
1. InputStream is used to open the file
2. BufferedReader is used to extract lines from the input stream and assemble them into a string
3. The string is then set as the text of the EditText
Writing to internal file
OutputStreamWriter is used to write the text from the EditText to the file using openFileOutput(FILE_NAME, 0)
The file's path is: /data/data/packageName/FileName
You may delete an existing resource file using the conventional .delete() method
SD cards
Offer larger capacity and portability
Many devices allow SD cards to be easily removed and reused in another device
Ideal for keeping music, pictures, ebooks, and video files
Use the File Explorer tool to locate files in your device (or emulator) in the mnt/sdcard/ folder
When dealing with external files you need to request permission to read and write to the SD card by adding the following clauses to your AndroidManifest.xml:
It is a better practice to determine the SD location using Environment.getExternalStorageDirectory().getAbsolutePath() rather than using a specific path
Method to determine the path to the external SD card
PrintWriter
Object used to send data tokens to disk using methods: print(), println(), printf()
Scanner
Accepts whitespace separated tokens and converts them to their corresponding types using methods: next(), nextInt(), nextDouble(), etc.
You want to use the method Environment.getExternalStorageDirectory().getPath() to determine the path to the external SD card
A PrintWriter object is used to send data tokens to disk using any of the following methods: print(), println(), printf()
A Scanner accepts whitespace separated tokens and converts them to their corresponding types using methods: next(), nextInt(), nextDouble(), etc.
Persistence
A strategy that allows the reusing of volatile objects and other data items by storing them into a permanent storage system such as disk files and databases
File IO management in Android
Includes the familiar IO Java classes: Streams, Scanner, PrintWriter, and so on
Permanent files
Can be stored internally in the device's main memory (usually small, but not volatile) or externally in the much larger SD card