SoFunction
Updated on 2025-04-07

Analysis of the difference between resource directory assets and res/raw in Android development

This article describes the difference between the resource directory assets and res/raw for Android development. Share it for your reference, as follows:

assets: Used to store static files that need to be packaged into the application for deployment to the device. The difference from res/raw is that ASSETS supports subdirectories of any depth. These files will not generate any resource IDs and must use the relative pathname to start with /assets (not including it).

res: The resources used to store applications (such as icons, GUI layouts, etc.) will be packaged into compiled Java. Depth subdirectories are not supported

res/menu: Store XML-based menu description;

res/raw: Store common files, and the files in this folder will not be compiled into binary files and will be copied to the device as is.

res/values:Storing strings and size values.

res/xml: Store common XML files

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

In Android development, we cannot do without the use of resource files. From drawable to string to layout, these resources provide great convenience for our development, but the resource catalog we usually contact most of the time is generally the following three.

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

But there are more than these android resource files. Let me introduce to you the other three resource directories below

/res/xml
/res/raw
/assets

First of all, /res/xml, you may have used this directory occasionally. It can be used to store xml format files, and it is the same as other resource files.The resources here will be compiled into binary format and placed in the final installation package. We can also access the files here through R class and parse the contents inside., for example, we store a file called:

<?xml version="1.0" encoding="utf-8"?>
<root>
 <title>Hello XML!</title>
</root>

Then, we can access and parse the file through the resource ID

XmlResourceParser xml = getResources().getXml();
();
int eventType = ();
boolean inTitle = false;
while(eventType != XmlPullParser.END_DOCUMENT) {
 //Sign it when you reach the title node if(eventType == XmlPullParser.START_TAG) {
  if(().equals("title")) {
   inTitle = true;
  }
 }
 //If the node that reaches the mark is passed, the content will be retrieved if(eventType ==  &amp;&amp; inTitle) {
  ((TextView)findViewById()).setText(
    ()
  );
 }
 ();
 eventType = ();
}

Here, 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 the SAX method. It should be noted that the xml file here will eventually be compiled into binary form. If you want the file to be stored as it is, then you have to use the next directory, that is, the /res/raw directory

The only difference between this directory is thatThe files here will be stored intact on the device and will not be compiled into binary form. The access method is also through R class, Here is an example:

((TextView)findViewById()).setText(
 readStream(getResources().openRawResource())
);
private String readStream(InputStream is) {
 try {
  ByteArrayOutputStream bo = new ByteArrayOutputStream();
  int i = ();
  while(i != -1) {
   (i);
   i = ();
  }
  return ();
 } catch (IOException e) {
  return "";
 }
}

This time, we use the method in the resource class, openRawResource, to return us an input stream, so that we can read the content in the file at will, for example, in the above example, output the content in the text file as it is.

Of course, if you need higher degrees of freedom and try not to be bound by the Android platform, then the /assets directory is your first choice.

In addition to not being compiled into binary form, the other thing in the file in this directory is that the access method is through the file name, not the resource ID. And more importantly, you can create subdirectories at will here, 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()).setText(
 readStream((""))
);

In the context context, call getAssets to return an AssetManager, and then use the open method to access the required resources. Here the open method is rooted in the assets directory. So the above code accesses the resource file named in the assets directory.

I hope this article will be helpful to everyone's Android programming design.