你想像
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的程序也逐步移植完成了。