如之前的篇章一样,先上截图
Read Asset 如同标题一样,本Activity主要是能够将要读取的文件放到assets目录下,再通过代码去读取显示到界面。
也就是常说的读取assets资源文件
下面看下布局和代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="normal"/> </LinearLayout> public class ReadAsset extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.read_asset); try {//打开资源文件 InputStream is = getAssets().open("read_asset.txt"); //定义文件的大小 int size = is.available(); //定义一个字符数组,用于存放文件中读出的内容 //将文件中的内容读入到buffer当中 byte[] buffer = new byte[size]; is.read(buffer); is.close(); //将二进制数组中的内容转化为字符串 String text = new String(buffer); TextView tv = (TextView)findViewById(R.id.text); tv.setText(text); } catch (IOException e) { throw new RuntimeException(e); } } } 再看下Assets目录至此,Content-Assets读取资源文件的功能就完成了。