原文 http://www.3geye.net/?3/viewspace-3021
你想像j2me那樣直接把所有的資源文件放在src目錄下面然后通過getClass().getResourceAsStream(name)這么簡單就可以獲取資源文件嗎。
答案是很肯定的。下面看看代碼吧。
importcom.google.android.samples.R;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.TextView;
importjava.io.IOException;
importjava.io.InputStream;
/**
* Demonstration of styled text resources.
*/
publicclassReadAssetextendsActivity
{
@Override
protectedvoidonCreate(Bundleicicle)
{
super.onCreate(icicle);
// See assets/res/any/layout/styled_text.xml for this
// view layout definition.
setContentView(R.layout.read_asset);
// Programmatically load text from an asset and place it into the
// text view. Note that the text we are loading is ASCII, so we
// need to convert it to UTF-16.
try{
InputStreamis=getAssets().open("read_asset.txt");
// We guarantee that the available method returns the total
// size of the asset... of course, this does mean that a single
// asset can't be more than 2 gigs.
intsize=is.available();
// Read the entire asset into a local byte buffer.
byte[]buffer=newbyte[size];
is.read(buffer);
is.close();
// Convert the buffer into a Java string.
Stringtext=newString(buffer);
// Finally stick the string into the text view.
TextViewtv=(TextView)findViewById(R.id.text);
tv.setText(text);
}catch(IOExceptione){
// Should never happen!
thrownewRuntimeException(e);
}
}
}
你只需要把你的資源文件放到assets目錄下面。一切就是那么的簡單,容易。我開始有點喜歡Android。
好了,我的J2ME的程序也逐步移植完成了。