`
iaiai
  • 浏览: 2148605 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用getIdentifier()获取资源Id

 
阅读更多
使用getIdentifier()方法可以方便的获各应用包下的指定资源ID。
主要有两种方法:
(1)方式一
Resources resources = context.getResources();
int indentify = resources.getIdentifier(org.loveandroid.androidtest:drawable/icon",null,null);
if(indentify>0){
icon = resources.getDrawable(indentify);
}

第一个参数格式是:包名 + : +资源文件夹名 + / +资源名;是这种格式 然后其他的可以为null

(2)方式二
Resources resources = context.getResources();
intindentify= getResources().getIdentifier("icon", "drawable", "org.anddev.android.testproject");

第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名。

如果找到了,返回资源Id,如果找不到,返回0 。

写了一个方法:获取资源ID,如果不存在返回0
static int getResourceId(Context context,String name,Stringtype,String packageName){
 
      ResourcesthemeResources=null;
      PackageManager pm=context.getPackageManager();
       try {
         themeResources=pm.getResourcesForApplication(packageName);
          returnthemeResources.getIdentifier(name, type, packageName);
       } catch(NameNotFoundException e) {

         e.printStackTrace();
       }
       return0;
 }


从数据库里读取图片名称,然后调用图片。直接用R.drawable.?无法调用。查了好多地方最后找到了个方法,分享给大家,希望有帮助。
主要由两种方法,个人建议第二种。
1.不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为:
String path = "com/drawable/resource/imageName.png";
InputStream is = getClassLoader().getResourceAsStream(path);
Drawable.createFromStream(is, "src");


2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了:
假设创建工程的时候,填写的package名字为:com.test.image
int resID = getResources().getIdentifier("imageName", "drawable","com.test.image");
Drawable image = getResources().getDrawable(resID);
分享到:
评论

相关推荐

    Android使用getIdentifier()获取资源Id的方法

    本文实例讲述了Android使用getIdentifier()获取资源Id的方法。分享给大家供大家参考,具体如下: int i= getResources().getIdentifier(icon, drawable, getPackageName()) ; if(i>0) {Log.i(aa,aa);} else {Log....

    Android中实现根据资源名获取资源ID

    接触过Android开发的同学们都知道在Android中访问程序资源...在这种情况下,使用文件名来得到资源ID显得事半功倍。 通过调用Resources的getIdentifier可以很轻松地得到资源ID。 几个简单的示例: 代码如下: Resources

    getIdentifierSample.rar

    使用getIdentifier()方法根据资源名来获取资源id

    CustomResManage.java

    通过资源名称,资源类型,程序包名获取资源的映射ID,通过对应的资源获取方法,获取该ID对应的资源对象 例如: 获取string资源: int id = context.getResources().getIdentifier(iconName, "string", context....

    SqInject:基于getIdentifier实现的Android控件注入库

    SqInject目录一,功能说明二,接收说明三,使用范例四,工程模块说明五,版本说明一,功能说明1,使用BindView将属性和id绑定(绑定采用的是getIdentifier的方式绑定的,非R.id) 2,BindId将id的值注入到属性中...

    yfz_getIdentifier.zip

    Android安卓-获取控件的id(标识符)-根据控件id获取对应的值或控件名字-动态获取R.string的值

    基于Android中获取资源的id和url方法总结

    一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: <”OK”>> 客户端请求成功 << / string>> //string资源中 名称为OK的 id 获取方式 int strId = getResources().getIdentifier(OK, ...

    android 获取APP的唯一标识applicationId的实例

    使用getIdentifier()方法可以方便的获各应用包下的指定资源ID。 方式一 int indentify = getResources().getIdentifier(“com.test.demo:drawable/icon”,null,null); 第一个参数格式是:包名 + : +资源文件夹名 +...

    android电子签名,屏幕上手写签名

    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } 3,...

    swipeback-Android侧滑返回分析和实现(不高仿微信).zip

    { int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); try { return getResources().getDimensionPixelSize(resourceId);  } catch (Resources....

    C# 获取硬件参数的实现方法

    C# 获取硬件参数的实现方法 示例代码: private static string GetIdentifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ; System.Management.ManagementClass mc = new ...

    SerialAndParallelExecutor

    要串行执行任务,请使用扩展 {@link SerialRunnable} 的可运行调用 {@link #execute(Runnable)}。 执行器将保证以串行顺序执行任何具有相同标识符的可运行程序。 所有其他未实现 {@link SerialRunnable} 的任务将以...

    dinnerplanner-android-NTH_YS

    交互编程 - 实验室作业 -... getIdentifier(name, " drawable " , context . getPackageName()); } 然后在您的视图类中,您有一个要将图像传递给的组件(例如 ),然后执行以下操作: ImageView img = // You either

Global site tag (gtag.js) - Google Analytics