Analysis of the differences between assets and res and raw resource directories developed by Android

  • 2020-12-16 06:07:55
  • OfStack

This article illustrates the differences between assets and res/raw. To share for your reference, the details are as follows:

assets: Used to store static files that need to be packaged into an application for deployment to a device. Unlike res/raw, ASSETS supports subdirectories of any depth. These files do not generate any resource ID and must start with the relative pathname /assets (excluding it).

res: The resources used to hold the application (such as ICONS, GUI layout, and so on) are packaged into the compiled Java. Depth subdirectories are not supported

res/menu: Store XML-based menu description;

res/raw: To store generic files, the files in this folder will not be compiled into base 2 files and will be copied to the device as is.

res/values: Store string and size values.

res/xml: Store common XML files

3 special resource directories /res/xml /res/raw and /assets

In the development of android, we cannot do without the use of resource files. From drawable to string and then to layout, these resources provide great convenience for our development. However, the following three resource directories are generally the ones we contact most of the time.

/res/drawable
/res/values
/res/layout

But android's resource files don't stop there. Here are three other resource directories

/res/xml
/res/raw
/assets

First is/res xml, this directory you may occasionally used, here can be used to store xml format file, and 1 sample files and other resources, the resources here is will be compiled into two hexadecimal format on the final installation package, we can also through R class to access the file here, and parse the contents inside, for instance, we had one store here called data. xml file:


<?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) {
 // arrive title Node time mark 1 Under the 
 if(eventType == XmlPullParser.START_TAG) {
  if(xml.getName().equals("title")) {
   inTitle = true;
  }
 }
 // If the node reaches the mark, fetch the contents 
 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 to the 1 xml parser, the working principle of the parser and almost SAX way, note that here xml file, will eventually be compiled into 2 hexadecimal form, if you want the file to the same storage, then use a directory, under the/res raw directory

The only difference in this directory is that the files will be stored on the device intact, will not be compiled to base 2, and accessed 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 using the method in the resource class, openRawResource, returns us with an input stream so that we can read the contents of the file as we saw in the example above, and output the contents of the text file as is.

Of course, if you need more freedom and are not bound by the android platform, then /assets is your first choice.

Apart from the fact that the files in this directory will not be compiled in base 2, the other point is that they are accessed through the file name instead of the resource ID. What's more important is that you can create any subdirectory in this directory, while the /res resource files cannot create their own subdirectories. If you need this kind of flexible resource storage, consider this example:


AssetManager assets = getAssets();
((TextView)findViewById(R.id.txAssets)).setText(
 readStream(assets.open("data.txt"))
);

In the CONTEXT of context, calling getAssets returns 1 AssetManager, and you can then access the required resources using the open method, which is rooted in the assets directory. So the code above accesses the resource file named ES112en.txt in the assets directory.

I hope this article is helpful for Android programming.


Related articles: