Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Development: File System Approaches Richard S. Stansbury 2015.

Similar presentations


Presentation on theme: "Android Development: File System Approaches Richard S. Stansbury 2015."— Presentation transcript:

1 Android Development: File System Approaches Richard S. Stansbury 2015

2 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

3 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

4 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 http://developer.android.com/reference/org/xmlpull/v1/X mlPullParser.html

5 Packaged Files: Assets How? – Create a folder in your root folder called “assets/” – AssetManager am = getAssets(); – InputStream is = am.open( ); e.g. “content/blah.html” would access the path and folder specified in the “assets/” folder Or, – For WebView w.loadURL(file://android_asset/ );file://android_asset/ Loads the content as HTML into the webview

6 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)); }

7 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

8 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)

9 File-System Storage: Internal No Path: Pass file names to: – FileInputStream fis = openFileInput( ) – FileOutputStream fos = openFileOutput(, int mode) Mode options – http://developer.android.com/reference/android/content/Co ntext.html#openFileOutput(java.lang.String, int) For paths: – Create file reference to root folder – Create file reference for sub-folder and filename

10 File-System Storage: Internal Within context – File root = getActivity().getFilesDir(); Or getActivity().getCacheDir(); – File file = new File(root, ); Now, you can create input stream or output writer (or the like) using the file File object.

11 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();

12 File-System Storage: External External storage in root folder – File root = Environment.getExternalStorageDirectory();

13 File-System Storage: External Public directory (for things to share) – File root = Environment.getExternalPublicDirectory( ) : Environment. – DIRECTORY_MUSIC – DIRECTORY_PODCASTS – DIRECTORY_RINGTONES – DIRECTORY_ALARMS – DIRECTORY_NOTIFICATIONS – DIRECTORY_PICTURES – DIRECTORY_MOVIES – DIRECTORY_DOWNLOADS – DIRECTORY_DCIM

14 File-System Storage: Permissions Manifest.xml – If you do have write permissions, you do not need to include read permissions (but not vice-versa)

15 File-System Storage: Multiple Users 4.2 and later now allows multiple users – getFilesDir() and getFilesExternalDir() provides separate folders for each user

16 References Textbook Resources: – http://developer.android.com/reference/android/cont ent/res/Resources.html XML Pull Parser: – http://developer.android.com/reference/org/xmlpull/ v1/XmlPullParser.html http://developer.android.com/reference/org/xmlpull/ v1/XmlPullParser.html Asset Manager – http://developer.android.com/reference/android/cont ent/res/AssetManager.html http://developer.android.com/reference/android/cont ent/res/AssetManager.html


Download ppt "Android Development: File System Approaches Richard S. Stansbury 2015."

Similar presentations


Ads by Google