0

This is how we get a resource identifier for a drawable that belongs to our app

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

But how do we do it when the drawable is not ours?
1. It belongs to android
2. It belongs to another app (I'm guessing that this is done by just using the other app's package name)

Anonymous
  • 4,470
  • 3
  • 36
  • 67
  • 1
    Why would you not just use the generated resource tables, e.g. `myPackageName.R.drawable.resourceName` and `android.R.drawable.resourceName`? – alanv Mar 08 '15 at 08:01
  • yes i figured it out after some MORE staring at the screen scratching my head... – Anonymous Mar 08 '15 at 12:41

1 Answers1

0

This is my solution and it works for me..Assuming that the iconResName is in the form of: packageName+":drawable/"+drawableName; I get the resources Id with the following function i wrote. And then we can use this resId to get the 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;}    
}
Anonymous
  • 4,470
  • 3
  • 36
  • 67