Working with Assets

Android offers one more directory where you can keep files to be included in the package: /assets. It's at the same level as /res, meaning it's not part of the /res subdirectories. The files in /assets do not generate IDs in R.java; you must specify the file path to read them. The file path is a relative path starting at /assets. You use the AssetManager class to access these files, as shown in Listing 3–34.

Listing 3–34. Reading an Asset

//Note: Exceptions are not shown in the code String getStringFromAssetFile(Activity activity){     AssetManager am = activity.getAssets();     InputStream is = am.open("test.txt");     String s = convertStreamToString(is);     is.close();     return s; }

Get Pro Android 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.