前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android根据资源名获取资源ID

Android根据资源名获取资源ID

作者头像
技术小黑屋
发布2018-09-04 17:09:53
3.4K0
发布2018-09-04 17:09:53
举报
文章被收录于专栏:技术小黑屋技术小黑屋

接触过Android开发的同学们都知道在Android中访问程序资源基本都是通过资源ID来访问。这样开发起来很简单,并且可以不去考虑各种分辨率,语言等不同资源显式指定。

痛点

但是,有时候也会有一些问题,比如我们根据服务器端的值取图片,但是服务器端绝对不会返回给我们的是资源id,最多是一种和文件名相关联的值,操作资源少的时候,可以维护一个容器进行值与资源ID的映射,但是多的话,就需要另想办法了。

便捷的方法

在这种情况下,使用文件名来得到资源ID显得事半功倍。 通过调用Resources的getIdentifier可以很轻松地得到资源ID。 几个简单的示例

lineos:false

1 2 3 4 5 6 7 8 9 10 11 12 13

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

运行结果

lineos:false

1

I/MainActivity( 4537): testGetResourceIds imageResId = 2130837504;imageResIdByAnotherForm = 2130837504;musicResId=2130968576;notFoundResId =0

看一看API

直接API

  • 这个方法用来使用资源名来获取资源ID
  • 完整的资源名为package:type/entry,如果资源名这个参数有完整地指定,后面的defType和defPackage可以省略。
  • defType和defPackage省略时,需要将其设置成null
  • 注意这个方法不提倡,因为直接通过资源ID访问资源会更加效率高
  • 如果资源没有找到,返回0,在Android资源ID中0不是合法的资源ID。

lineos:false android.content.res.Resources.class

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

/** * 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调用的是AssetManager.class中的native方法。

lineos:false

1 2 3 4 5 6

/** * Retrieve the resource identifier for the given resource name. */ /*package*/ native final int getResourceIdentifier(String type, String name, String defPackage);

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 痛点
    • 便捷的方法
    • 看一看API
      • 直接API
        • 间接API
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档