0

这就是我们获取属于我们应用程序的可绘制资源标识符的方式

int resID = getResources().getIdentifier("resourceName",
"drawable", "myPackageName");

但是当drawable不是我们的时候我们怎么做呢?
1.它属于android
2.它属于另一个应用程序(我猜这是通过使用另一个应用程序的包名来完成的)

4

1 回答 1

0

这是我的解决方案,它适用于我..假设 iconResName 的形式为:packageName+":drawable/"+drawableName; 我使用我编写的以下函数获取资源 ID。然后我们可以使用这个 resId 来获取 drawable。

public static int getResIdFromResEntryName(String iconResName,Context con){
  try{
    String packageName = iconResName.split(":")[0]; 
    Context otherAppContext = con.createPackageContext(packageName,Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY);
    Resources resources = otherAppContext.getResources();
    int resId = resources.getIdentifier(iconResName, null, null);
    return resId;
  }catch(Exception e){return 0;}    
}
于 2015-03-08T12:47:39.527 回答