日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

getResources()方法

發(fā)布時(shí)間:2025/3/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 getResources()方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
今天做一個(gè)Android文件管理器。它使用了很多當(dāng)?shù)氐挠猛緂etResources。

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()這種方法就能夠獲取存在系統(tǒng)的資源。于是看了一下文檔和翻閱了一下資料:

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

[java] view plaincopyprint?
Resources myResources = getResources();? ?
InputStream myFile = myResources.openRawResource(R.raw.myfilename);? ?
Resources myResources = getResources();
InputStream myFile = myResources.openRawResource(R.raw.myfilename);
?

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



簡寫


[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();? ?
//以數(shù)據(jù)流的方式讀取資源?? ?
Inputstream is = r.openRawResource(R.drawable.my_background_image);? ?
BitmapDrawable bmpDraw = new BitmapDrawable(is);? ?
Bitmap bmp = bmpDraw.getBitmap();? ?
//得到Resources對象
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();
?

或者


[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);
?

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

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

把一個(gè)圖片資源,加入你的文件到你project中res/drawable/文件夾中去,從這里。你就能夠引用它到你的代碼或你的XML布局中,也就是說。引用它也能夠用資源編號(hào),比方你選擇一個(gè)文件僅僅要去掉后綴就能夠了(比如:my_image.png 引用它是就是my_image)。

當(dāng)須要使用的xml資源的時(shí)候。就能夠使用context.getResources().getDrawable(R....資源的地址如:R.String.ok);

當(dāng)你方?jīng)]有法律Context參數(shù),能 this.getContext().getResources();這使。






注意,使用getResource()當(dāng)注意

1、必須有Context是啊 2、它可以用來作為成員變量,施工方法參數(shù)傳入或。

它。

轉(zhuǎn)載于:https://www.cnblogs.com/gcczhongduan/p/4591507.html

總結(jié)

以上是生活随笔為你收集整理的getResources()方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。