通过文件名获得资源ID,主要是图片.

2019-04-15 12:52发布

public int getImage(String pic)
{
if (pic == null || "".equals(pic.trim()))
{
return R.drawable.default_barner;
}
Class draw = R.drawable.class;
try
{
Field field = draw.getDeclaredField(pic);
return field.getInt(pic);
}
catch (SecurityException e)
{
return R.drawable.default_barner;
}
catch (NoSuchFieldException e)
{
return R.drawable.default_barner;
}
catch (IllegalArgumentException e)
{
return R.drawable.default_barner;
}
catch (IllegalAccessException e)
{
return R.drawable.default_barner;
}
} _______________      也是在网络上找着的方法,试用满足条件,所以记下来。