demibug

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            24 Posts :: 3 Stories :: 2 Comments :: 0 Trackbacks
          轉(zhuǎn)自 http://happyin1111.blog.163.com/


          Drawable currentIcon = null;

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

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

          把資源文件放到應(yīng)用程序的/raw/raw下,那么就可以在應(yīng)用中使用getResources獲取資源后,以openRawResource方法(不帶后綴的資源文件名)打開(kāi)這個(gè)文件。例如:

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

          和傳統(tǒng)的java文件操作一樣,在android Api中提供了openFileInput和openFileOutput方法來(lái)讀取設(shè)備上的文件。

          簡(jiǎn)寫(xiě)
          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對(duì)象
          Resources r = this.getContext().getResources();
          //以數(shù)據(jù)流的方式讀取資源
          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);

          數(shù)據(jù)包package:android.content.res
          主要類(lèi):Resources

          InputStream openRawResource(int id) 獲取資源的數(shù)據(jù)流,讀取資源數(shù)據(jù)

          把 一個(gè)圖片資源,添加你的文件到你工程中res/drawable/目錄中去,從這里,你就可以引用它到你的代碼或你的XML布局中,也就是說(shuō),引用它也可 以用資源編號(hào),比如你選擇一個(gè)文件只要去掉后綴就可以了(例如:my_image.png 引用它是就是my_image)。

          posted on 2011-05-09 16:30 Hiji 閱讀(1252) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 丹棱县| 乐亭县| 莱西市| 渭南市| 微博| 于都县| 江华| 尚志市| 周宁县| 随州市| 景洪市| 祁连县| 天气| 杭锦后旗| 高台县| 仁怀市| 尼木县| 湄潭县| 紫金县| 铁力市| 博兴县| 平顺县| 大同市| 洪雅县| 伊川县| 大名县| 凤庆县| 鄂伦春自治旗| 环江| 南阳市| 扎鲁特旗| 河津市| 南漳县| 张家川| 尉氏县| 临桂县| 马关县| 化隆| 山西省| 兴城市| 修水县|