Access to Android file resources (raw/data/asset)

Source: Internet
Author: User
Tags xml parser

In Android development, we can't do without the use of resource files. From drawable to string, to layout, these resources provide great convenience for our development, however, most of the resource directories we usually access are the following three.

/RES/drawable
/RES/Values
/RES/Layout


However, there are more than these resource files in Android. The following describes the other three resource directories.

/RES/XML
/RES/raw
/Assets


The first is/RES/XML. This directory may be used occasionally. It can be used to store files in XML format and is the same as other resource files, the resources here will be compiled into binary format and put into the final installation package. We can also access the files here through the R class and parse the content, for example, we store a file named data. XML file:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Root>
  3. <Title> Hello XML! </Title>
  4. </Root>

Copy code

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

  1. Xmlresourceparser xml = getresources (). getxml (R. xml. data );
  2. XML. Next ();
  3. Int eventtype = xml. geteventtype ();
  4. Boolean intitle = false;
  5. While (eventtype! = Xmlpullparser. end_document ){
  6. // Mark when the title node is reached
  7. If (eventtype = xmlpullparser. start_tag ){
  8. If (XML. getname (). Equals ("title ")){
  9. Intitle = true;
  10. }
  11. }
  12. // If the mark is reached, the content is taken out.
  13. If (eventtype = xmlpullparser. Text & intitle ){
  14. (Textview) findviewbyid (R. Id. txxml). settext (
  15. XML. gettext ()
  16. );
  17. }
  18. XML. Next ();
  19. Eventtype = xml. geteventtype ();
  20. }

Copy code

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, for details about sax, see my other post:
Http://www.eoeandroid.com/thread-33212-1-1.html

Note that the XML file will eventually be compiled into binary format. If you want to store the file as is, you need to use the next directory, that is the/RES/raw directory.

The only difference between this directory is that the files here will be stored on the device and won't be compiled in binary format. The access method is also through the R class. The following is an example:

  1. (Textview) findviewbyid (R. Id. txraw). settext (
  2. Readstream (getresources (). openrawresource (R. Raw. rawtext ))
  3. );
  4. Private string readstream (inputstream is ){
  5. Try {
  6. Bytearrayoutputstream BO = new bytearrayoutputstream ();
  7. Int I = is. Read ();
  8. While (I! =-1 ){
  9. Bo. Write (I );
  10. I = is. Read ();
  11. }
  12. Return Bo. tostring ();
  13. } Catch (ioexception e ){
  14. Return "";
  15. }
  16. }

Copy code

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

Of course, if you need higher degrees of freedom and try not to be bound by the Android platform, the/Assets Directory is your first choice ~
The files in this directory will not be compiled into binary format. In addition, the access method is through the file name rather than the resource ID. More importantly, you can create sub-directories here, and resource files in the/RES directory cannot create sub-directories by yourself. If you need this flexible resource storage method, let's look at the following example:

  1. Assetmanager assets = getassets ();
  2. (Textview) findviewbyid (R. Id. txassets). settext (
  3. Readstream (assets. Open ("data.txt "))
  4. );

Copy code

In the context, call getassets to return an assetmanager, and then use the open method to access the required resources. Here, the open method is based on the assets directory. The following code indicates the resource file named data.txt in the assetsdirectory ~

These three directories are useful when we develop applications. They are sent to you here, hoping to help you develop applications ~

 

 

 

Access to Android file resources (raw/data/asset)

 

1. File Access in a private folder (/data/package name)

Java code

  1. ImportJava. Io. fileinputstream;
  2. ImportJava. Io. fileoutputstream;
  3. ImportOrg. Apache. http. util. encodingutils;
  4. Public voidWritefiledata (string filename, string message ){
  5. Try{
  6. Fileoutputstream fout = openfileoutput (filename, mode_private );
  7. Byte[] Bytes = message. getbytes ();
  8. Fout. Write (bytes );
  9. Fout. Close ();
  10. }
  11. Catch(Exception e ){
  12. E. printstacktrace ();
  13. }
  14. }
  15. PublicString readfiledata (string filename ){
  16. String res = "";
  17. Try{
  18. Fileinputstream fin = openfileinput (filename );
  19. IntLength = fin. Available ();
  20. Byte[] Buffer =New byte[Length];
  21. Fin. Read (buffer );
  22. Res = encodingutils. getstring (buffer, "UTF-8 ");
  23. Fin. Close ();
  24. }
  25. Catch(Exception e ){
  26. E. printstacktrace ();
  27. }
  28. ReturnRes;
  29. }

 

 

2. Get the file from the raw folder in the resource and read the data (the resource file can only be read and cannot be written)

Java code

  1. PublicString getfromraw (string filename ){
  2. String res = "";
  3. Try{
  4. Inputstream in = getresources (). openrawresource (R. Raw. test1 );
  5. IntLength = in. Available ();
  6. Byte[] Buffer =New byte[Length];
  7. In. Read (buffer );
  8. Res = encodingutils. getstring (buffer, "UTF-8 ");
  9. In. Close ();
  10. }
  11. Catch(Exception e ){
  12. E. printstacktrace ();
  13. }
  14. ReturnRes;
  15. }

3. Get files from asset and read data (resource files can only be read but cannot be written)

Java code

  1. PublicString getfromasset (string filename ){
  2. String res = "";
  3. Try{
  4. Inputstream in = getresources (). getassets (). Open (filename );
  5. IntLength = in. Available ();
  6. Byte[] Buffer =New byte[Length];
  7. In. Read (buffer );
  8. Res = encodingutils. getstring (buffer, "UTF-8 ");
  9. }
  10. Catch(Exception e ){
  11. E. printstacktrace ();
  12. }
  13. ReturnRes;
  14. }
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.