获取应用程序图标作为int标识符

时间:2011-12-17 18:56:12

标签: android

我想要检索应用的图标。如果我使用

String pkg = "com.app.my";
Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);

结果是Drawable。如何检索关联资源的id以将其作为意图中的额外资源传递?谢谢!

4 个答案:

答案 0 :(得分:4)

以下代码段可以解决问题。

PackageManager pm = getPackageManager();
String pkg = "com.app.my";

try {
    ApplicationInfo ai = pm.getApplicationInfo(pkg, 0);
    int iconId = ai.icon;
} catch (NameNotFoundException e) {
    // ...
}

答案 1 :(得分:2)

             s=name of the drawable
             int id=getResources().getIdentifier(s,"drawable",getPackageName());

            you will get the id here    

答案 2 :(得分:1)

使用

getResources().getIdentifier() from your Context (e.g., Activity)

答案 3 :(得分:1)

您的意思是其他应用的图标,还是您所在的应用?

如果它在你所在的应用程序中,那么这应该足够吗?

    int i = R.drawable.icon;