Android Development:File System Approaches
Richard S. Stansbury2015
File System-based Data Storage
• Packaged Files– Raw Resources– XML Resources– Assets
• Internal and External Storage– Options
• Application Specific Internal• Application Specific External• Public Shared Storage
– Permission required for external write and/or read
Packaged Files: Raw Resources
• What are raw resources?– Data files of arbitrary format
• How are they included?– Stored in the res/raw folder– Example res/raw/blah.txt– Resources r = getResources();– InputStream i = r.openRawResource(R.raw.blah);
• What can be done to/with them? – Read only– Support’s reference of static application data
Packaged Files: XML Resources
• Why?– If your static application data is XML, then better to include
here than under raw.• How?
– Data stored in “res/xml/”– e.g. res/xml/blah.xml
• Resources r = getResources();• XMLResourceParser xml = r.getXml(R.xml.blah);
• What do I do with this?– Return’s an XML Pull Parser– http://developer.android.com/reference/org/xmlpull/v1/X
mlPullParser.html
Packaged Files: Assets
• How? – Create a folder in your root folder called “assets/”– AssetManager am = getAssets();– InputStream is = am.open(<path>);
• e.g. “content/blah.html” would access the path and folder specified in the “assets/” folder
• Or,– For WebView
• w.loadURL(file://android_asset/<path>); • Loads the content as HTML into the webview
Example: Assets
public void loadDatabase() throws IOException{
AssetManager am = getAssets();InputStream inputStream = am.open("movies.csv");BufferedReader input = new BufferedReader(new
InputStreamReader(inputStream));
String movieStr;while ((movieStr = input.readLine()) != null) {
_mdb.addMovie(Movie.parseMovie(movieStr));}
}
File-system Storage
• Access files for read/write at runtime– Persistent data
• Data that will remain for the life of your application (or longer)
– Cached data• Data that is eligible for purge periodically by other applications /
system.
• You can open instances java.io classes such as File, InputStream, OutputWriter, etc.– Some issues must be considered on how to specify the path
• Internal vs. External options
File-System Storage: Internal
• Onboard and always available• Typically the built-in flash– Note: some devices have internal flash for both
the “internal storage” and “external storage”• e.g. Nexus 7
• Data only available to the application– Not accessible by other applications or external
means (Note: exceptions for rooted devices)
File-System Storage: Internal
• No Path: Pass file names to:– FileInputStream fis = openFileInput(<filename>)– FileOutputStream fos =
openFileOutput(<filename>, int mode)• Mode options
– http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)
• For paths:– Create file reference to root folder– Create file reference for sub-folder and filename
File-System Storage: Internal
• Within context– File root = getActivity().getFilesDir();• Or getActivity().getCacheDir();
– File file = new File(root, <FILENAME>);• Now, you can create input stream or output
writer (or the like) using the file File object.
File-System Storage: External
• Application specific storage folder– Accessible by all– File root = getActivity().getExternalFilesDir();
• Application specific cache folder– Accessible by all, but eligible for random sweep– File root = getActivity().getExternalCacheDir();
File-System Storage: External
• External storage in root folder– File root =
Environment.getExternalStorageDirectory();
File-System Storage: External
• Public directory (for things to share)– File root =
Environment.getExternalPublicDirectory(<type>)• <type>: Environment.
– DIRECTORY_MUSIC– DIRECTORY_PODCASTS– DIRECTORY_RINGTONES – DIRECTORY_ALARMS– DIRECTORY_NOTIFICATIONS– DIRECTORY_PICTURES– DIRECTORY_MOVIES– DIRECTORY_DOWNLOADS– DIRECTORY_DCIM
File-System Storage: Permissions
• Manifest.xml– <uses-permission
android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />– <uses-permission
android:name=“android.permission.READ_EXTERNAL_STORAGE” />
• If you do have write permissions, you do not need to include read permissions (but not vice-versa)
File-System Storage: Multiple Users
• 4.2 and later now allows multiple users– getFilesDir() and getFilesExternalDir() provides
separate folders for each user
References
• Textbook• Resources:– http://developer.android.com/reference/android/content/
res/Resources.html• XML Pull Parser:– http://developer.android.com/reference/org/xmlpull/v1/
XmlPullParser.html• Asset Manager– http
://developer.android.com/reference/android/content/res/AssetManager.html
Top Related