Persistent Storage in Android Applications

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

MODE_PRIVATE

By setting this mode, the file can only be accessed using calling application

MODE_ENABLE_WRITE_AHEAD_LOGGING

Database open flag. When it is set , it would enable write ahead logging by default

apply()

Editor class method: It is an abstract method. It will commit your changes back from editor to the sharedPreference object you are calling

clear()

Editor class method: It will remove all values from the editor

remove(String key)

Editor class method: It will remove the value whose key has been passed as a parameter

putFloat(String key, float value)

Editor class method: It will save a float value in a preference editor

putInt(String key, int value)

Editor class method: It will save a integer value in a preference editor

putLong(String key, long value)

Editor class method: It will save a long value in a preference editor

read(byte[) buffer, int byteOffset, int byteCount)

FileInputStream Methods: This method reads at most length bytes from this stream and stores them in the byte array b starting at offset

available()

FileInputStream Methods: This method returns an estimated number of bytes that can be read or skipped without blocking for more input

reading file

FileOutputStream Methods: FileInputStream fin = openFileInput(file); int c; String temp = ""; while( (c = fin.read()) != -1) { temp = temp + Character.toString((char)c); } //string temp contains all the data of the file. fin.close();

write(byte[) buffer, int byteOffset, int byteCount)

FileOutputStream Methods: This method Writes count bytes from the byte array buffer starting at position offset to this stream

FileOutputStream(File file, boolean append)

FileOutputStream Methods: This method constructs a new FileOutputStream that writes to file.

FileInputStream Methods: getChannel()

This method returns a read-only FileChannel that shares its position with this stream

FileOutputStream Methods: getChannel()

This method returns a write-only FileChannel that shares its position with this stream

FileInputStream Methods: getFD()

This method returns the underlying file descriptor

FileOutputStream Methods: getFD()

This method returns the underlying file descriptor

MODE_MULTI_PROCESS

This method will check for modification of preferences even if the shared preference instance has already been loaded

MODE_WORLD_READABLE

This mode allow other application to read the preferences

MODE_WORLD_WRITEABLE

This mode allow other application to write the preferences

MODE_APPEND

This will append the new preferences with the already existing preferences

Public Files

— Files that should be freely available to other apps and to the end user. Example: photos captured by your app or other downloaded files — not deleted when user uninstalls the app

Shared Preferences

— Store private primitive data in key-value pairs — use to save any primitive data: boolean, float, int, long, and string which will persist across user sessions (even if your application is killed) — Generally used to store small amount of data, that may be required frequently

Context.MODE_APPEND

keep appending data to the existing contents

getExternalFilesDir()

method to store private files on external storage

SharedPreferences class

provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types

getExternalCacheDir()

store cache files on external storage that are removed when the app is uninstalled and not accessible when SD card is removed

getExternalStorageDirectory()

to get to the root of the external storage in API < 7

Private Files

— Files that rightfully belong to your app and should be deleted when the user uninstalls your app. They don't provide value to the user outside your app. Example: Other resources downloaded by your app. — deleted when user uninstalls the app

writing file

String FILENAME = "mytext.txt"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); byte[] buf = string.getBytes(); fos.write(buf); fos.close();

Network Connection

Store data on the web with your own network server

External Storage

Store private data on the device external memory

SQLite Database

Store structured data in a private database

Context.MODE_PRIVATE

cannot be read by others

getExternalStorageFilesDir(String type)

create files specific to your app that are removed when your app is uninstalled.

Writing to External Storage

—Add permission statement to your manifest file. WRITE_EXTERNAL_STORAGE —Check the external storage to see if its MOUNTED, READ_ONLY, or FAILURE using getExternalStorageState() method. —Get the files directory by calling getExternalStorageFilesDir(String type) or getExternalStoragePublicDirectory(String type) or getExternalStorageDirectory() —Use a (FileInputStream) and (FileOutputStream) to do the required operations.

Internal Storage comparison

—It's always available. —Files saved here are accessible by only your app. —When the user uninstalls your app, the system removes all your app's files from internal storage. —Less memory available. —Access speed is more or less the same.

External Storage comparison

—It's not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device. —It's world-readable, so files saved here may be read outside of your control. —When the user uninstalls your app, the system removes your app's files only if you save them in the directory returned by getExternalFilesDir(). —Larger memory size. Asst. Prof. Alex A. Santos

Internal Storage

—Store private data on the device memory —allows reading and writing to files which are associated with each application's internal memory —files are private to the application by default, hence they can only be accessed by the application who created them —files are automatically deleted once the host application is uninstalled —other apps and users cannot browse your internal directories and don't have read/write access unless you explicitly set the files to be readable/writable —when you use MODE_PRIVATE for your files on the internal storage, they are never accessible to other apps — best when you want to be sure that neither the user nor other apps can access your files

Internal/External Storage

—Storing data and files to the phone's external Secure Digital (SD) card. —Less efficient

SharedPreferences

—Storing data internally in XML files. —More efficient due to less read/write overhead. —Stores only primitive types and at most, arrays of Strings

public static File getExternalFilesDir(String type)

—returns the absolute path to the directory on the primary external where the application can place persistent files it owns. —these files are private to the applications, and not typically visible to the user as media —type = may be null for the root of the files directory or one of the following Environment constants for a subdirectory: DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_DOWNLOADS, DIRECTORY_PICTURES, DIRECTORY_MOVIES. —if type in not null, the returned file will a path to a sub-directory of the given type —get a top-level public external storage directory for placing files of a particular type —user will typically place and manage their own files, so you should be careful about what put here to ensure your don't erase their files or get in the way of their own organization. —on devices with multiple users, each user has their own isolated external storage. —applications only have access to the external storage for the currently active user.

getExternalStoragePublicDirectory()

—store files not specific to your app and should not be deleted when your app is uninstalled. —method to store public files on external storage

Cache Storage

—files stored in the files folder are persistent and remain until they are deleted —files can be stored temporarily (caching) —use getCacheDir() or getExternalCacheDir() to get a reference to the absolute path of your app's cache directory —use the File class to create a file inside the cache folder —the files are stored in data/data/<package-name>/cache folder —when the device is low on internal storage space, Android may delete these cache files to recover space —do not rely on the system to clean up these files

Methods to access External Storage

• getExternalStorageDirectory() • getExternalStorageFilesDir(String type) • getExternalCacheDir() • getExternalStoragePublicDirectory()


Ensembles d'études connexes

Interpersonal Communication Final (Exam 2 Q's)

View Set

IBA Final Practice I-A, I-B, I-C

View Set

EAQ- Lewis Med Surg CH.27, Nursing Management: Upper Respiratory Problems- Problems of Pharynx, Trachea, and Larynx

View Set

eCampus Student Orientation - Test out (D2L/Brightspace Pulse)

View Set

understanding Business - chapter 12

View Set