2014-1-13

Source: Internet
Author: User

There are several different places to get the values in the String.xml file:

1. In XML files such as Androidmanifest.xml and layout:

android:text= "@string/resource_name"

2. In the activity:

Method One: This.getstring (r.string.resource_name);

Method Two: Getresources (). getString (R.string.resource_name);

3. In other Java files (must have context or pplication)

Method One: Context.getstring (r.string.resource_name);

Method Two: Application.getstring (r.string.resource_name);

Assets Access to Folder resources

The files inside the assets folder are kept in the original file format and need to be read by Assetmanager in the form of a byte stream.

1. First call Getassets () in the activity to get the Assetmanager reference.

2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and the access mode to get the input stream inputstream.

3. Then you read the file with the InputStream of the open file, and remember to Inputstream.close () after the read is done.

4. Call Assetmanager.close () to close Assetmanager.

It is important to note that files from resources and assets can only be read and cannot be written.

The following is read from the raw file:

Public String Getfromraw () {

try {

InputStreamReader Inputreader = new InputStreamReader (Getresources (). Openrawresource (R.raw.test1));

BufferedReader bufreader = new BufferedReader (Inputreader);

String line= "";

String result= "";

while (line = Bufreader.readline ()) = null)

Result + = line;

return Result;

} catch (Exception e) {

E.printstacktrace ();

}

}

The following is read directly from assets

public string Getfromassets (string fileName) {

try {

InputStreamReader Inputreader = new InputStreamReader (Getresources (). Getassets (). Open (FileName));

BufferedReader bufreader = new BufferedReader (Inputreader);

String line= "";

String result= "";

while (line = Bufreader.readline ()) = null)

Result + = line;

return Result;

} catch (Exception e) {

E.printstacktrace ();

}

}

2014-1-13

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.