先说问题的场景吧:
项目要用到的图片都放在Drawable中,然后将图片的名称保存在数据库中。所以需要通过数据库中的名字找到ID;
解决思路:
这样对图片等资源的读取可以通过:首先在数据库中查找到对应的资源名称,然后通过反射找到资源对应的ID,这样就可以通过使用资源的ID,引用资源了。
解决办法:
使用 getResources().getIdentifier("资源名称","资源类型", getApplicationContext().getPackageName())方法;
方法参数说明:
int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)
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.
返回给定资源名称的资源标识(这里我的理解是资源I