The difference analysis between assets and Res/raw in the resource catalogue of Android development _android

Source: Internet
Author: User
Tags xml parser

This article illustrates the difference between the assets and Res/raw of the resource directory of Android development. Share to everyone for your reference, specific as follows:

assets: Used to store static files that need to be packaged into applications for deployment to a device. Unlike Res/raw, assets supports subdirectories of any depth. These files do not generate any resource IDs and must use the relative pathname of the/assets start (not including IT).

Res: Resources for storing applications, such as icons, GUI layouts, and so on, will be packaged into the compiled java. Depth subdirectory not supported

Res/menu: Store the xml-based menu description;

Res/raw: Stores common files, files within that folder will not be compiled into binary files and copied to the device as-is.

res/values: Holds string, dimension value.

res/xml: Storing common XML files

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

In Android development, we can not do without the use of resource files, from drawable to string, and then to layout, these resources for our development has provided a great convenience, but we usually contact the most of the resource directory is generally the following three.

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

But Android's resource files are more than that, and here's a list of three other resources.

/res/xml
/res/raw
/assets

The first is/res/xml, this directory may be used occasionally, this can be used to store XML-formatted files, and as with other resource files, the resources here will be compiled into a binary format into the final installation package, we can also through the R class to access the files here, and parsing the contents , for example, where we have a file called Data.xml:

<?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 (r.xml.data);
Xml.next ();
int eventtype = Xml.geteventtype ();
Boolean intitle = false;
while (EventType!= xmlpullparser.end_document) {
 ///When the title node is reached, mark the
 if (EventType = = Xmlpullparser.start_tag {
  if (Xml.getname (). Equals ("title")) {
   intitle = true;
  }
 }
 If the node that has reached the tag takes out the content
 if (eventtype = Xmlpullparser.text && intitle) {(
  TextView) Findviewbyid ( R.id.txxml)). SetText (
    xml.gettext ())
  ;
 }
 Xml.next ();
 EventType = Xml.geteventtype ();
}

Here, we use the Getxml method of the resource class, returns an XML parser that works like a sax, and note that the XML file here will eventually be compiled into binary form, and if you want the file to be stored as it is, the next directory will be used. That's the/res/raw catalogue.

The only difference in this directory is that the file will be stored intact on the device, will not be compiled into binary form, accessed in the same way through the R class , the following 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 an input stream so that we can read the contents of the file arbitrarily, as in the example above, to output the contents of the text file as it is.

Of course, if you need higher degrees of freedom and try not to be constrained 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 filename, not the resource ID. And the more important point is that you can create subdirectories here arbitrarily, and the resource files in the/res directory cannot build subdirectories on their own . If you need this flexible way of storing resources, look at the following example:

Assetmanager assets = getassets ();
((TextView) Findviewbyid (r.id.txassets)). SetText (Readstream (
 "Assets.open"))
;

In the context, calling Getassets returns a Assetmanager and then uses the open method to access the resource that is needed, where the open method is rooted in the assets directory. So the above code accesses the resource file named Data.txt in the assets directory.

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.