The Android Resource Directory---assets differs from Res/raw
Android 2011-05-24 14:40:21 Read 20 Comments 0 font Size: Big Small Subscription
Assets: Used to store static files that need to be packaged into the application for deployment to the device. Unlike Res/raw, assets supports subdirectories of any depth. These files do not generate any resource IDs, and you must use the relative pathname of/assets to start (not include it).
Res: the resources used to store the application (superscript, GUI layout, etc.) will be packaged in the compiled Java. Deep subdirectories are not supported
Res/menu: Storing XML-based menu descriptions;
Res/raw: Store A generic file, the files within that folder will not be compiled into binaries and copied to the device as is.
Res/values: Store string, dimension value.
Res/xml: Storing a common XML file
Three special resource directories/res/xml/res/raw and/assets
In the development of Android, we can not leave the use of resource files, from drawable to string, to layout, these resources for our development to provide a great convenience, but we usually most of the time in contact with the resource directory is generally the following three.
/res/drawable
/res/values
/res/layout
But the Android resource files are more than this, let's introduce the other three resources directory
/res/xml
/res/raw
/assets
The first is/res/xml, this directory may be used occasionally, this can be used to store XML format files, and other resource files, where the resources will be compiled into a binary format in the final installation package, we can also through the R class to access the file here, and parse the contents , for example, we have stored a file named Data.xml here:
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<title>hello xml!</title>
</root>
We can then access and parse the file through the resource ID.
Xmlresourceparser XML = Getresources (). GETXML (R.xml.data);
Xml.next ();
int eventtype = Xml.geteventtype ();
Boolean intitle = false;
while (eventtype! = xmlpullparser.end_document) {
Mark when you reach the title node
if (EventType = = Xmlpullparser.start_tag) {
if (Xml.getname (). Equals ("title")) {
Intitle = true;
}
}
Take out the content if the node that arrives at the mark
if (EventType = = Xmlpullparser.text && intitle) {
((TextView) Findviewbyid (R.id.txxml)). SetText (
Xml.gettext ()
);
}
Xml.next ();
EventType = Xml.geteventtype ();
}
Here, we use the resource class Getxml method, returned an XML parser, this parser works similar to the sax way, note that the XML file here will eventually be compiled into binary form, if you want to let the file is stored as it is, then we need to use the next directory, That's the/res/raw directory.
The only difference in this directory is that the files here are stored intact on the device, not compiled into binary form, and accessed in the same way through the R class, here's an example:
((TextView) Findviewbyid (R.id.txraw)). SetText (
Readstream (Getresources (). Openrawresource (R.raw.rawtext))
);
Private String Readstream (InputStream is) {
try {
Bytearrayoutputstream bo = new Bytearrayoutputstream ();
int i = Is.read ();
while (i! =-1) {
Bo.write (i);
i = Is.read ();
}
return bo.tostring ();
} catch (IOException e) {
Return "";
}
}
This time using the method in the resource class, Openrawresource, returns us an input stream so that we can read the contents of the file arbitrarily, as in the example above, and output the contents of the text file as is.
Of course, if you need a higher degree of freedom, try not to be bound by the Android platform, then/assets this directory is your first choice ~
The files in this directory are not compiled into binary form, and the other is that the access is through the file name, not the resource ID. And more importantly, you can create subdirectories here arbitrarily, and the resource files in the/res directory are not self-creating subdirectories. If you need this kind of flexible resource storage, take a look at the following example:
Assetmanager assets = getassets ();
((TextView) Findviewbyid (r.id.txassets)). SetText (
Readstream (Assets.open ("Data.txt"))
);
In context, calling Getassets returns a Assetmanager and then uses the open method to access the required resources, where the open method is rooted in the assets directory. So the above code accesses a resource file named Data.txt in the assets directory ~
Excerpt from: http://blog.csdn.net/hshm20517/article/details/6461890
Android Resource Directory---assets and res/raw differences