The Android system provides a/assets folder for each newly designed program. The files saved in this folder can be packaged in the program. The difference between/RES and/assets is that android does not generate an ID for the file in/assets. Assume that the file under/assets needs to be specified. The following example shows how to configure the content of assets.
Create a/image subfolder in the/assets file and copy the icon.png subfolder under/RES/drawableto this folder. Create the readme.txt file in the/assetsfolder, and enter the text "Hello, world!" in the file !!!".
Layout file: Main. xml
<? XML version ="1.0"Encoding ="UTF-8"?>
<Linearlayoutxmlns: Android =Http://schemas.android.com/apk/res/android"
Android: Orientation ="Vertical"
Android: layout_width ="Fill_parent"
Android: layout_height ="Fill_parent"
>
<Textview
Android: layout_width ="Fill_parent"
Android: layout_height ="Wrap_content"
Android: text ="@ String/hello"
/>
<Edittext Android: Id ="@ + ID/firstid"
Android: layout_width ="Fill_parent"
Android: layout_height ="Wrap_content"
Android: text ="@ String/hello"
/>
<Edittext Android: Id ="@ + ID/secondid"
Android: layout_width ="Fill_parent"
Android: layout_height ="Wrap_content"
Android: text ="@ String/hello"
/>
</Linearlayout>
Program file:
PackageCom.cn. getassets;
ImportAndroid. App. activity;
ImportAndroid. OS. Bundle;
ImportJava. Io. bytearrayoutputstream;
ImportJava. Io. ioexception;
ImportJava. Io. inputstream;
ImportAndroid. App. activity;
ImportAndroid. content. res. assetmanager;
ImportAndroid. OS. Bundle;
ImportAndroid. util. log;
ImportAndroid. widget. edittext;
Public ClassGetassetsExtendsActivity {
PrivateEdittextfirstfield;
PrivateEdittextsecondfield;
@ Override
Public VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
// Log. D ("showmain. xml", "OK ");
Setcontentview (R. layout.Main);
Log.D("Showmain. xml", "OK ");
Assetmanager = getassets ();
String [] files =Null;
Try{
Files = assetmanager. List ("image ");
}Catch(Ioexception e ){
Log.E("Tag", E. getmessage ());
}
Firstfield = (edittext) findviewbyid (R. Id.Firstid);
Firstfield. settext (integer.Tostring(Files. Length) + "file. filename is" + files [0]);
Inputstream =Null;
Try{
Inputstream = assetmanager. Open ("readme.txt ");
}Catch(Ioexception e ){
Log.E("Tag", E. getmessage ());
}
String S = readtextfile (inputstream );
Secondfield = (edittext) findviewbyid (R. Id.Secondid);
Secondfield. settext (s );
}
PrivateStringreadtextfile (inputstream ){
Bytearrayoutputstreamoutputstream =NewBytearrayoutputstream ();
ByteBuf [] =NewByte[1024];
IntLen;
Try{
While(LEN = inputstream. Read (BUF ))! =-1 ){
Outputstream. Write (BUF, 0, Len );
}
Outputstream. Close ();
Inputstream. Close ();
}Catch(Ioexception e ){
}
ReturnOutputstream. tostring ();
}
}
Program display result: Use the simulator.
Http://blog.sina.com.cn/s/blog_6cf0d3f30100m2x6.html
Use of assetmanager asset