android getResources的作用

 今天做一个Android的文件管理器,里面用到很多的地方用到了getResources。


  1. Drawable currentIcon = null;  
  2.   
  3. currentIcon = getResources().getDrawable(R.drawable.folder);  
  4.   
  5. currentIcon = getResources().getDrawable(R.drawable.image);  

       一开始不是很理解为什么用 getResources()这个方法就可以获取存在系统的资源。于是看了一下文档和翻阅了一下资料:

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


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

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

简写


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

读取res/drawable目录下的png或者bmg


  1. //得到Resources对象  
  2. Resources r = this.getContext().getResources();  
  3. //以数据流的方式读取资源  
  4. Inputstream is = r.openRawResource(R.drawable.my_background_image);  
  5. BitmapDrawable bmpDraw = new BitmapDrawable(is);  
  6. Bitmap bmp = bmpDraw.getBitmap();  

转载于:https://my.oschina.net/u/874134/blog/158606

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值