getResource() android

例如:把资源文件放到应用程序的/raw/raw下,那么就可以在应用中使用getResources获取资源后,以openRawResource方法(不带后缀的资源文件名)打开这个文件。例如:

Resources myResources = getResources();
InputStream myFile = myResources.openRawResource(R.raw.myfilename);

和传统的java文件操作一样,在android Api中提供了openFileInput和openFileOutput方法来读取设备上的文件。

简写
InputStream fs =this.getResources().openRawResource(R.raw.kb); (资源文件名为kb.html, 不需要带后缀.html)
InputStreamReader read = new InputStreamReader (fs,”gb2312″);
BufferedReader in = new BufferedReader(read);

读取res/drawable目录下的png或者bmg
//得到Resources对象
Resources r = this.getContext().getResources();
//以数据流的方式读取资源
Inputstream is = r.openRawResource(R.drawable.my_background_image);
BitmapDrawable bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();

或者
InputStream is = getResources().openRawResource(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
Paint mPaint = new Paint();
canvas.drawBitmap(mBitmap, 40, 40, mPaint);

数据包package:android.content.res
主要类:Resources

InputStream openRawResource(int id) 获取资源的数据流,读取资源数据

把一个图片资源,添加你的文件到你工程中res/drawable/目录中去,从这里,你就可以引用它到你的代码或你的XML布局中,也就是说,引用它也可以用资源编号,比如你选择一个文件只要去掉后缀就可以了(例如:my_image.png 引用它是就是my_image)。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值