Android basic res file assets xml raw

xml file

You may use this directory occasionally. It can be used to store files in xml format, and like other resource files, the resources here will be compiled into binary format and placed in the final installation package. We can also
pass The R class accesses the files here and parses the contents inside. For example, we store a file named data.xml here:
<?xml version="1.0" encoding="utf-8"?>
<root>
	<title>Hello XML!</title>
</root>

Then, we can access and parse this file by resource ID:
XmlResourceParser xml = getResources().getXml(R.xml.data);
xml.next();
int eventType = xml.getEventType();
boolean inTitle = false;
while(eventType != XmlPullParser.END_DOCUMENT) {
	//到达title节点时标记一下
	if(eventType == XmlPullParser.START_TAG) {
		if(xml.getName().equals("title")) {
			inTitle = true;
		}
	}
	
	//如过到达标记的节点则取出内容
	if(eventType == XmlPullParser.TEXT && inTitle) {
		((TextView)findViewById(R.id.txXml)).setText(
			xml.getText()
		);
	}
	
	xml.next();
	eventType = xml.getEventType();
}
We use the getXml method of the resource class to return an xml parser. The working principle of this parser is similar to that of SAX

SAX(Simple API for XML)

It is a widely used XML parsing standard. Handler mode is usually used to process XML documents. This processing mode is very different from the way we are used to understanding. There are often some friends around me who feel a little comprehensible when they first come into contact with SAX
. difficulty. In fact, SAX is not complicated, it is just a way of thinking,
as its name indicates, in order to allow us to process XML documents in a simpler way




raw

It should be noted that the xml file here will eventually be compiled into a binary form. If you want to store the file as it is, then you need to use the next directory, which is
the The difference is that the files here will be stored on the device intact and will not be compiled into binary form, and the access method is also through the R class. Here is 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, use the method in the resource class, openRawResource, to return us an input stream, so that we can read the contents of the file arbitrarily, such as in the above example, output the contents of the text file as it is.
 

assets

Of course, if you need a higher degree of freedom and try not to be constrained by the android platform, then the /assets directory is your first choice~ Except that the
files in this directory will not be compiled into binary form, another point is, Access is by filename, not resource ID.
And the more important point is that you can create subdirectories here arbitrarily, and the resource files in the /res directory cannot create subdirectories by themselves.
If you need this flexible resource storage method, then take a look at the following example:
AssetManager assets = getAssets();
((TextView)findViewById(R.id.txAssets)).setText(
		readStream(assets.open("data.txt"))
);

difference one

The difference between android raw and assets
*The same points between res/raw and assets:
1. The files in the two directories will be saved in the apk package intact after packaging, and will not be compiled into binary.


*The difference between res/raw and assets:
1. The files in res/raw will be mapped to the R.java file, and the resource ID, namely R.id.filename, will be used directly when accessing; the files under the assets folder will not It is mapped to R.java, and the AssetManager class is required for access.
2. res/raw cannot have a directory structure, while assets can have a directory structure, that is, folders can be created under the assets directory


/difference two

One, the same point:
some files smaller than 1M can be placed in the assets and res/raw project directories (required before version 2.3, otherwise the data will not be read.), these files will be packaged into the APK application.


Second, the difference:
the assets directory
The files in the assets directory will be packaged without any processing.
The files in the assets directory will not be mapped to R.java, that is, the system will not automatically generate a resource ID for it.
The files in the assets directory need to be accessed with the help of AssetManager.
The assets directory allows multiple levels of subdirectories below.
es/raw directory 
1. Some documents will be compiled into binary, and some documents will not 
2. The system will generate corresponding resource IDs for all resources in the res directory, and files in raw are no exception. 
3. No directory structure is allowed under res/raw.


3. Example of reading files:
1.assets directory
AssetManager a = getAssets(); 
//fileName is the name of the file to be accessed in the assets directory 
InputStream is = a.open(fileName); 
//then you can use the input stream to Read the content of fileName. 
In addition, we can obtain the information of the fileName file, such as length, etc., through the AssetFileDescriptor object obtained by a.openFd(fileName).
We can also get the array of all files and subdirectory names under the assets directory through a.list(""), and get the array of all files and subdirectory names under assets/SubFolderName through a.list(SubFolderName).


2.res/raw directory
InputStream is = getResources().openRawResource(R.id.fileNameID); 
//R.id.fileNameID is the resource ID corresponding to the file to be accessed. Then we can read the corresponding file through the input stream The content of the file is gone.
 
 

Guess you like

Origin blog.csdn.net/l331258747/article/details/52872194