Example: Fetch resource file is displayed in ScrollView.
1.readasset.java
PackageCom.example.ReadAsset; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.widget.TextView; Importjava.io.IOException; ImportJava.io.InputStream; Public classReadassetextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.read_asset); Try { //Return an Assetmanager instance for your application ' s packageInputStream is = Getassets (). Open ("Index.txt"); intSize =is.available (); //Read the entire asset into a local byte buffer. byte[] buffer =New byte[size]; Is.read (buffer); Is.close (); //Convert the buffer into a string. String Text =NewString (buffer, "GB2312"); //Finally Stick the string into the text view. TextView TV =(TextView) Findviewbyid (R.id.text); Tv.settext (text); } Catch(IOException e) {//should never happen! Throw NewRuntimeException (e); } } }
2. Read_asset.xml
<?xml version= "1.0" encoding= "Utf-8"?> <scrollview android:layout_width= "fill_parent" xmlns: android= "Http://schemas.android.com/apk/res/android" android:layout_height= "Fill_parent" android:paddingtop= "50dip" > <textview android:id= "@+id/text" android:layout_width= "Fill_parent " android:layout_height= "Wrap_content" android:textstyle= "normal"/> </ScrollView>
3. Then create a new assets folder in the project, put a index.txt file in it, run
Android reads the contents of files under the Assert folder