Android获取所有应用的资源id和对应的uri
生活随笔
收集整理的這篇文章主要介紹了
Android获取所有应用的资源id和对应的uri
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
背景在某些應(yīng)用中,為了實(shí)現(xiàn)應(yīng)用apk資源放入重復(fù)利用,或者使用反射得到本應(yīng)用的資源,需要使用反射方式獲得,但Resources類中也自帶了這種獲取方式,并且功能更加強(qiáng)大你可以獲取string,color,drawable,raw,xml等文件,因此也就意味著,這里可以獲取的資源是res中已定義的資源,對(duì)于控件id的獲取,暫時(shí)無法做到public int getIdentifier(String name, String defType, String defPackage) {if (name == null) {throw new NullPointerException("name is null");}try {return Integer.parseInt(name);} catch (Exception e) {// Ignore
}return mAssets.getResourceIdentifier(name, defType, defPackage);
}獲取資源的Id獲取當(dāng)前應(yīng)用的資源Idint drawableId = mContext.getResources().getIdentifier("ic_launcher","drawable", mContext.getPackageName());
mImageView.setImageResource(drawableId);獲取其他應(yīng)用的資源Idint id = mContext.getResources().getIdentifier("icon", "drawable", "com.android.testproject");
// 或者
int id = mContext.getResources().getIdentifier("com.android.testproject:drawable/icon", null, null);整合到一起public static int getResourceId(Context context, String name, String type, String packageName){Resources themeResources=null;PackageManager pm=context.getPackageManager();try {themeResources=pm.getResourcesForApplication(packageName);return themeResources.getIdentifier(name, type, packageName);} catch (NameNotFoundException e) {e.printStackTrace();}return 0;
}獲取系統(tǒng)資源Idint id = getResources().getIdentifier("actionbar_bg", "drawable","android"); //注意,最后一個(gè)參數(shù)必須是“android”
獲取資源的Uriandroid系統(tǒng)中,應(yīng)用的資源存儲(chǔ)時(shí)也通常會(huì)被存入 數(shù)據(jù)庫,也可以被共享,
因此來說資源也可以獲得uripublic static Uri getResourceUri(int resId,String packageName) {return Uri.parse("android.resource://"+packageName+"/"+resId);
}
?
總結(jié)
以上是生活随笔為你收集整理的Android获取所有应用的资源id和对应的uri的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: h5应用数据加密_邦伲德H5场景个性化开
- 下一篇: go channel 缓冲区最大限制_一