Android处理非res资源的例子

 你想像 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的程序也逐步移植完成了。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值