今天做一个Android文件管理器。它使用了很多当地的用途getResources。



Drawable currentIcon = null;   

   

………………   

   

currentIcon = getResources().getDrawable(R.drawable.folder);   

   

………………   

   

currentIcon = getResources().getDrawable(R.drawable.image);   

   

…………   

Drawable currentIcon = null;



………………



currentIcon = getResources().getDrawable(R.drawable.folder);



………………



currentIcon = getResources().getDrawable(R.drawable.image);



…………

 



一開始不是非常理解为什么用c getResources()这种方法就能够获取存在系统的资源。于是看了一下文档和翻阅了一下资料:



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



[java] view plaincopyprint?

Resources myResources = getResources();   

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

Resources myResources = getResources();

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

 



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

简写





[java] view plaincopyprint?

InputStream fs =this.getResources().openRawResource(R.raw.kb); (资源文件名称为kb.html, 不须要带后缀.html)   

InputStreamReader read = new InputStreamReader (fs,”gb2312″);   

BufferedReader in = new BufferedReader(read);   

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





[java] view plaincopyprint?

//得到Resources对象    

Resources r = this.getContext().getResources();   

//以数据流的方式读取资源    

Inputstream is = r.openRawResource(R.drawable.my_background_image);   

BitmapDrawable bmpDraw = new BitmapDrawable(is);   

Bitmap bmp = bmpDraw.getBitmap();   

//得到Resources对象

Resources r = this.getContext().getResources();

//以数据流的方式读取资源

Inputstream is = r.openRawResource(R.drawable.my_background_image);

BitmapDrawable bmpDraw = new BitmapDrawable(is);

Bitmap bmp = bmpDraw.getBitmap();

 



或者





[java] view plaincopyprint?

InputStream is = getResources().openRawResource(R.drawable.icon);   

Bitmap mBitmap = BitmapFactory.decodeStream(is);   

Paint mPaint = new Paint();   

canvas.drawBitmap(mBitmap, 40, 40, mPaint);   

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) 获取资源的数据流。读取资源数据



把一个图片资源,加入你的文件到你project中res/drawable/文件夹中去,从这里。你就能够引用它到你的代码或你的XML布局中,也就是说。引用它也能够用资源编号,比方你选择一个文件仅仅要去掉后缀就能够了(比如:my_image.png 引用它是就是my_image)。



当须要使用的xml资源的时候。就能够使用context.getResources().getDrawable(R....资源的地址如:R.String.ok);



当你方没有法律Context参数,能 this.getContext().getResources();这使。

注意,使用getResource()当注意



1、必须有Context是啊 2、它可以用来作为成员变量,施工方法参数传入或。

它。


getResources()方法的更多相关文章

  1. Android 非Activity类引用getResources()方法问题的解决方法

    在进行Android开发的过程中,在一个非Activity类(此处假设类名为MyNewClass)中引用了getResources()方法,如下: Bitmap bmp = BitmapFactory ...

  2. Android重写getResources规避用户调整系统字体大小影响Android屏幕适配

    Android屏幕适配一直是一个头疼的问题.除此之外还要考虑APP在实际应用场景中,用户千奇百怪的设置,最常见的用户设置行为就是设置手机的字体大小,比如把字体设置成超大或者超小,这对屏幕适配又带来额外 ...

  3. 003android初级篇之【转】Android开发中颜色的定义方法

    正好用到颜色的定义,但脑子里没有记住具体,转载一篇加强印象 1.使用Color类的常量,如: int color = Color.BLUE; // 创建一个蓝色 是使用Android提供的颜色 int ...

  4. obtainFreshBeanFactory方法源码跟踪

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...

  5. Class和ClassLoader的getResource方法对比

    最近在看写Spring的源代码,里面有好多地方都用到了Class和ClassLoader类的getResource方法来加载资源文件.之前对这两个类的这个方法一知半解,概念也很模糊,这边做下整理,加深 ...

  6. 基于tomcat与Spring的实现差异化配置方案

    起因 在实际开发过程中经常需要加载各种各样的配置文件..比如数据库的用户名密码,要加载的组件,bean等等..但是这种配置在各个环境中经常是不一样的....比如开发环境和测试环境,真实的生产环境.. ...

  7. classpath: VS classpath*:

    同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源 classpath*:需要遍历所有的 ...

  8. 读取java目录中相同目录、相同名称的文件

    使用ClassLoader的getResources方法(注意,不是getResource,少了s),可获得指定文件的包含jar包名称的多个路径值,然后依次读取文件即可. 使用class,只能通过ge ...

  9. Spring相关

    一.Spring中ApplicationContext加载机制加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet. 这两者在功能上完全等同,只是 ...

随机推荐

  1. vim :g

    https://stackoverflow.com/questions/1946738/vim-how-to-delete-every-second-row An elegant (and effic ...

  2. [Docker] Run, Stop and Remove Docker Containers

    In this lesson, we'll find out the basics of running Docker containers. We'll go over how to downloa ...

  3. Gradle自己定义插件

    Gradle自己定义插件 在Gradle中创建自己定义插件,Gradle提供了三种方式: 在build.gradle脚本中直接使用 在buildSrc中使用 在独立Module中使用 开发Gradle ...

  4. 基于bootstrap的富文本框——wangEditor【欢迎增加开发】

    先来一张效果图: 01. 引言 老早就開始研究富文本框的东西,在写完<深入理解javascript原型与闭包>之后,就想着要去做一个富文本框的插件的样例. 如今网络上开源的富文本框插件许多 ...

  5. CRT(C Runtime Library)—— C/C++运行时库

    C runtime library(part of the C standard library) 任何一个 C 程序,它的背后都有一套庞大的代码来进行支撑,使得该程序得以运行在更高级别上,而不必担心 ...

  6. RabbitMQ 服务

    RabbitMQ 使用场景一   安装环境 1.下载安装 Erlang 运行时环境 2.下载安装 RabbitMQ Server 应用程序 3.启动 RabbitMQ 服务(默认启动) 4.安装管理平 ...

  7. [Angular Directive] Structure directive and <template>

    The structure directive is just a sugar syntax of <template>. Such as: <div *ngIf="nam ...

  8. 在nginx中使用lua直接訪问mysql和memcaced达到数据接口的统一

    安装nginx參见<nginx+lua+redis构建高并发应用> 让nginx 中的nginx_lua_module支持mysql 和memcache 下载 https://github ...

  9. [iOS]iOS开发人员账号申请与注冊流程

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  10. PatentTips - Heterogeneous Parallel Primitives Programming Model

    BACKGROUND 1. Field of the Invention The present invention relates generally to a programming model ...