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:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Root>
- <Title> Hello XML! </Title>
- </Root>
Copy code
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 ){
- // Mark when the title node is reached
- If (eventtype = xmlpullparser. start_tag ){
- If (XML. getname (). Equals ("title ")){
- Intitle = true;
- }
- }
- // If the mark is reached, the content is taken out.
- If (eventtype = xmlpullparser. Text & intitle ){
- (Textview) findviewbyid (R. Id. txxml). settext (
- XML. gettext ()
- );
- }
- XML. Next ();
- Eventtype = xml. geteventtype ();
- }
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:
- (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 "";
- }
- }
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:
- Assetmanager assets = getassets ();
- (Textview) findviewbyid (R. Id. txassets). settext (
- Readstream (assets. Open ("data.txt "))
- );
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
- ImportJava. Io. fileinputstream;
- ImportJava. Io. fileoutputstream;
- ImportOrg. Apache. http. util. encodingutils;
- Public voidWritefiledata (string filename, string message ){
- Try{
- Fileoutputstream fout = openfileoutput (filename, mode_private );
- Byte[] Bytes = message. getbytes ();
- Fout. Write (bytes );
- Fout. Close ();
- }
- Catch(Exception e ){
- E. printstacktrace ();
- }
- }
- PublicString readfiledata (string filename ){
- String res = "";
- Try{
- Fileinputstream fin = openfileinput (filename );
- IntLength = fin. Available ();
- Byte[] Buffer =New byte[Length];
- Fin. Read (buffer );
- Res = encodingutils. getstring (buffer, "UTF-8 ");
- Fin. Close ();
- }
- Catch(Exception e ){
- E. printstacktrace ();
- }
- ReturnRes;
- }
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
- PublicString getfromraw (string filename ){
- String res = "";
- Try{
- Inputstream in = getresources (). openrawresource (R. Raw. test1 );
- IntLength = in. Available ();
- Byte[] Buffer =New byte[Length];
- In. Read (buffer );
- Res = encodingutils. getstring (buffer, "UTF-8 ");
- In. Close ();
- }
- Catch(Exception e ){
- E. printstacktrace ();
- }
- ReturnRes;
- }
3. Get files from asset and read data (resource files can only be read but cannot be written)
Java code
- PublicString getfromasset (string filename ){
- String res = "";
- Try{
- Inputstream in = getresources (). getassets (). Open (filename );
- IntLength = in. Available ();
- Byte[] Buffer =New byte[Length];
- In. Read (buffer );
- Res = encodingutils. getstring (buffer, "UTF-8 ");
- }
- Catch(Exception e ){
- E. printstacktrace ();
- }
- ReturnRes;
- }