1. Get the input stream for a resource
The resource file Sample.txt is located in the $PROJECT _home/assets/directory and can be used in the Activity
Context.getassets (). Open ("Sample.txt")
method to get the input stream.
Note: If the resource file is a text file, you need to consider the encoding and line breaks of the file. It is recommended to use UTF-8 and Unix line breaks.
2. WebView loading HTML files in the assets directory
The resource file sample.html is located in the $PROJECT _home/assets/directory, which is available through the following code
Webview.loadurl ("file:///android_asset/sample.html");
Loads the HTML file.
The Android system provides a/assets directory for each newly designed program, and the files stored in this directory can be packaged in a program. The difference between/res and/assets is that Android does not generate IDs for files under/assets. If you use a file under/assets, you need to specify the path and file name of the file. The following example shows how to access the content under/assets.
Create the/image subdirectory in the file/assets, and copy the Icon.png subdirectory under/res/drawable to the directory. Create the Readme.txt file in the/assets subdirectory, and enter the text "Hello,world!!!" in the file.
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns: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 Files:
Package com.cn.getassets;
Import android.app.Activity;
Import Android.os.Bundle;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import android.app.Activity;
Import Android.content.res.AssetManager;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.widget.EditText;
public class Getassets extends Activity {
Private EditText Firstfield;
Private EditText Secondfield;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
LOG.D ("Show Main.xml", "OK");
Setcontentview (R.layout.main);
LOG.D ("Show Main.xml", "OK");
Assetmanager 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. File name is "+ files[0]);
InputStream 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);
}
Private String ReadTextFile (InputStream inputstream) {
Bytearrayoutputstream outputstream = new Bytearrayoutputstream ();
byte buf[] = new byte[1024];
int Len;
try {
while (len = Inputstream.read (BUF))! =-1) {
Outputstream.write (buf, 0, Len);
}
Outputstream.close ();
Inputstream.close ();
} catch (IOException e) {
}
return outputstream.tostring ();
}
}
Android reads resources under the assets directory