Android中實現(xiàn)根據(jù)資源名獲取資源ID
接觸過Android開發(fā)的同學(xué)們都知道在Android中訪問程序資源基本都是通過資源ID來訪問。這樣開發(fā)起來很簡單,并且可以不去考慮各種分辨率,語言等不同資源顯式指定。
痛點
但是,有時候也會有一些問題,比如我們根據(jù)服務(wù)器端的值取圖片,但是服務(wù)器端絕對不會返回給我們的是資源id,最多是一種和文件名相關(guān)聯(lián)的值,操作資源少的時候,可以維護(hù)一個容器進(jìn)行值與資源ID的映射,但是多的話,就需要另想辦法了。
便捷的方法
在這種情況下,使用文件名來得到資源ID顯得事半功倍。 通過調(diào)用Resources的getIdentifier可以很輕松地得到資源ID。 幾個簡單的示例:
Resources res = getResources();
final String packageName = getPackageName();
int imageResId = res.getIdentifier("ic_launcher", "drawable", packageName);
int imageResIdByAnotherForm = res.getIdentifier(packageName + ":drawable/ic_launcher", null, null);
int musicResId = res.getIdentifier("test", "raw", packageName);
int notFoundResId = res.getIdentifier("activity_main", "drawable", packageName);
Log.i(LOGTAG, "testGetResourceIds imageResId = " + imageResId
+ ";imageResIdByAnotherForm = " + imageResIdByAnotherForm
+ ";musicResId=" + musicResId
+ ";notFoundResId =" + notFoundResId);
運行結(jié)果
I/MainActivity( 4537): testGetResourceIds imageResId = 2130837504;imageResIdByAnotherForm = 2130837504;musicResId=2130968576;notFoundResId =0
看一看API
直接API
1.這個方法用來使用資源名來獲取資源ID
2.完整的資源名為package:type/entry,如果資源名這個參數(shù)有完整地指定,后面的defType和defPackage可以省略。
3.defType和defPackage省略時,需要將其設(shè)置成null
4.注意這個方法不提倡,因為直接通過資源ID訪問資源會更加效率高
5.如果資源沒有找到,返回0,在Android資源ID中0不是合法的資源ID。
**
* Return a resource identifier for the given resource name. A fully
* qualified resource name is of the form "package:type/entry". The first
* two components (package and type) are optional if defType and
* defPackage, respectively, are specified here.
*
* <p>Note: use of this function is discouraged. It is much more
* efficient to retrieve resources by identifier than by name.
*
* @param name The name of the desired resource.
* @param defType Optional default resource type to find, if "type/" is
* not included in the name. Can be null to require an
* explicit type.
* @param defPackage Optional default package to find, if "package:" is
* not included in the name. Can be null to require an
* explicit package.
*
* @return int The associated resource identifier. Returns 0 if no such
* resource was found. (0 is not a valid resource ID.)
*/
public int getIdentifier(String name, String defType, String defPackage) {
try {
return Integer.parseInt(name);
} catch (Exception e) {
// Ignore
}
return mAssets.getResourceIdentifier(name, defType, defPackage);
}
間接API
實際上上述API調(diào)用的是AssetManager.class中的native方法。
/**
* Retrieve the resource identifier for the given resource name.
*/
/*package*/ native final int getResourceIdentifier(String type,
String name,
String defPackage);
相關(guān)文章
利用Android中BitmapShader制作自帶邊框的圓形頭像
這篇文章給大家介紹了一下如何利用BitmapShader制作圓形頭像,可以自定義要顯示的圖片,邊框顏色和邊框?qū)挾鹊?,有需要的朋友們可以參考借鑒。2016-09-09Android編程實現(xiàn)仿易信精美彈出框效果【附demo源碼下載】
這篇文章主要介紹了Android編程實現(xiàn)仿易信精美彈出框效果,涉及Android窗口及動畫操作相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01完全解析Android多線程中線程池ThreadPool的原理和使用
本篇文章給大家通過原理和實例詳細(xì)講述了Android多線程中線程池ThreadPool的原理和使用,對此有興趣的朋友可以跟著參考學(xué)習(xí)下。2018-04-04Flutter實現(xiàn)一個支持漸變背景的Button示例詳解
這篇文章主要為大家介紹了Flutter實現(xiàn)一個支持漸變背景的Button示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09android studio3.0以上如何通過navicat訪問SQLite數(shù)據(jù)庫文件
這篇文章主要介紹了android studio3.0以上如何通過navicat訪問SQLite數(shù)據(jù)庫文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06