我們把資料放進去這個Assets資料夾內必須透過AssetManager的getAssets方法去取得
下方這些文字是我放在Assets資料夾內的test.txt檔案
Hello Android Blog Name:Smile.Net Blog:http://writecodepeople.blogspot.tw
package com.cyfang.assets; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class AssetsMain extends Activity { private TextView textView = null; private Button btn_show = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_assets_main); init(); } private void init() { textView = (TextView) findViewById(R.id.textView1); btn_show = (Button) findViewById(R.id.button1); btn_show.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { readAssetsData(); } }); } private void readAssetsData() { try { InputStream stream = getAssets().open("test.txt"); int size = stream.available(); byte[] bs = new byte[size]; stream.read(bs); stream.close(); textView.setText(new String(bs)); } catch (IOException e) { e.printStackTrace(); } } }
參考資料:
http://developer.android.com/reference/android/content/res/Resources.html