Get resource ID of the icon of another android application

你。 提交于 2019-12-08 03:49:14

问题


Is there a way to get the resource ID of the icon of another android app? (ex. com.android.systemui.R.drawable.ic_xxx)

I tried context.getApplicationInfo().icon but it returned a long integer. Is this possible?

Thanks


回答1:


You can get the Drawable icon of an app by using:

Drawable icon = getPackageManager().getApplicationIcon( PACKAGE_NAME );

If you are interested in the resource ID of a Drawable of your own app, try this:

int resourceID = getResources().getIdentifier( DRAWABLE_NAME , "drawable", PACKAGE_NAME );

Or, if you care about performance, this version is quicker, but uses reflection:

try {
      Class resource = R.drawable.class;
      Field field = resource.getField( DRAWABLE_NAME );
      int drawableId = field.getInt(null);
    } catch (Exception e) {}



回答2:


This method should work for getting the app icon of an application, including yours:

String appPackageName=...; //use getPackageName() in case you wish to use yours
final PackageManager pm=getPackageManager();
final ApplicationInfo applicationInfo=pm.getApplicationInfo(packageName,PackageManager.GET_META_DATA);
final Resources resources=packageManager.getResourcesForApplication(applicationInfo);
final Bitmap appIconBitmap=BitmapFactory.decodeResource(resources,applicationInfo.icon);


来源:https://stackoverflow.com/questions/11961599/get-resource-id-of-the-icon-of-another-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!