zoukankan      html  css  js  c++  java
  • getResources()方法

    今天做一个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、它可以用来作为成员变量,施工方法参数传入或。

    它。

  • 相关阅读:
    共享无法访问问题,通过ip地址或者主机名无法访问目的主机
    开机系统更新,一直停在?%处,无法进入系统
    win7电脑访问内网地址报错0x800704cf,0x80070035解决方法
    电脑共享--问题汇总
    win10域账户用户时间无法和域服务器同步
    卸载WPS后,原office出现各种问题,报错,图标混乱
    局域网新装电脑主机网络断断连连解决方案
    win10主机无法进入本地共享,“没有权限”
    win10安装部分软件报错“应用程序无法启动,应用程序并行配置不正确,或使用命令行sxstrace.exe”
    【日常修机】打印机故障维护
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4591507.html
Copyright © 2011-2022 走看看