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