2008-03-08
Android处理非res资源的例子
原文 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的程序也逐步移植完成了。
发表评论
- 浏览: 562653 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
我女朋友
共 12 张
共 12 张
最新评论
-
Google产品大全
很全很全很全很全
-- by sheandwei -
说真的UCWEB看奥运真方便
楼下的。话不能这么说。我现在上班开奥运就靠它了,
-- by wuhua -
说真的UCWEB看奥运真方便
傻佬,这也值得说
-- by caohj -
UCWEB使用秘诀
最近发现UCWEB有准备发布新版本了。到时再仔细研究相关的一些特殊用法。
-- by wuhua -
开始进入另外一个领域
自己的水平还很菜啊。要努力学习
-- by wuhua






评论排行榜