Resource file storage for Android Development

Source: Internet
Author: User

Storage operations on resource files in Android development. In android development, resource files are frequently used, including string, drawable, and layout, it also provides a lot of convenience for our start, but the resource directories we usually access are generally the following three. In addition to/res/drawable/res/values/res/layout, the following three types of Android resource files are frequently used: /res/xml/res/raw/assets is first/res/xml. This directory may be used occasionally. It can be used to store files in xml format, and like 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, a file named file. xml is stored here: <? Xml version = "1.0" encoding = "UTF-8"?> <Book> <title> XML advanced programming </<span> title> </<span> book> later, we can access and parse the file through the resource ID, the following code shows XmlResourceParser xml = getResources (). getXml (R. xml. file); xml. next (); int eventType = xml. getEventType (); boolean inTitle = false; while (eventType! = XmlPullParser. END_DOCUMENT) {// when the title node is reached, mark if (eventType = XmlPullParser. START_TAG) {if (xml. getName (). equals ("title") {inTitle = true ;}// if the node has reached the mark, 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 to return an xml parser. The working principle of this parser is similar to that of the SAX method. 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 only difference between the/res/raw directory and the directory is that the files here will be stored on the device and won't be compiled into binary format. The access method is also through the R class, the following is a sample code: (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 an input stream to us, in this way, the content in the file can be read at will. For example, in the above example, the content in the text file is output as is. Of course, if you need a higher degree of freedom and try not to be bound by the android platform, the/assets Directory is our 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, take a look at the following example: AssetManager assets = getAssets (); (TextView) findViewById (R. id. txAssets )). setText (readStream (assets. open ("data.txt"); 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. The Android file resource (raw/data/asset) access code is as follows: 1. File Access in a private folder (/data/package name) import java. io. fileInputStream; import java. io. fileOutputStream; import org. apache. http. util. encodingUtils; public void writeFileData (String fileName, String message) {try {FileOutputStream fout = openFileOutput (fileName, MODE_PRIVATE); byte [] bytes = message. getBytes (); fout. write (bytes); fout. close ();} catch (Exception e) {e. printS TackTrace () ;}} public String readFileData (String fileName) {String res = ""; try {FileInputStream fin = openFileInput (fileName); int length = fin. available (); byte [] buffer = new byte [length]; fin. read (buffer); res = EncodingUtils. getString (buffer, "UTF-8"); fin. close ();} catch (Exception e) {e. printStackTrace ();} return res;} 2. Get the file from the raw folder in the resource and read the data (the resource file can only be read but cannot be written) public String getFromRaw (S Tring fileName) {String res = ""; try {InputStream in = getResources (). openRawResource (R. raw. test1); int length = in. available (); byte [] buffer = new byte [length]; in. read (buffer); res = EncodingUtils. getString (buffer, "UTF-8"); in. close ();} catch (Exception e) {e. printStackTrace ();} return res;} 3. Get the file from the asset and read the data (the resource file can only be read but cannot be written) public String getFromAsset (String fileName) {String res = ""; try {In PutStream in = getResources (). getAssets (). open (fileName); int length = in. available (); byte [] buffer = new byte [length]; in. read (buffer); res = EncodingUtils. getString (buffer, "UTF-8");} catch (Exception e) {e. printStackTrace ();} return res;} There are the following methods for Android to obtain the absolute path of assets: Method 1: String path = "file: // android_asset/file name "; method 2: InputStream abpath = getClass (). getResourceAsStream ("/assets/file name "); To convert data to the String type, use the following code: String path = new String (InputStreamToByte (abpath); private byte [] InputStreamToByte (InputStream is) throws IOException {ByteArrayOutputStream bytestream = new ByteArrayOutputStream (); int ch; while (ch = is. read ())! =-1) {bytestream. write (ch);} byte imgdata [] = bytestream. toByteArray (); bytestream. close (); return imgdata ;}

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.