应用程序的图标可以通过以下代码获得:
Drawable drawable = ResolveInfo.loadIcon(packageManager);
但是它可能是由ROM修改的(如:直角改为圆角),所以我的问题是如何通过程序获得原始徽标图标?
P.S。一些启动器可以做到这一点,比如MIUI启动器。
答案 0 :(得分:3)
您可以使用PackageManager
PackageManager manager = context.getPackageManager();
Drawable appIcon = manager.getApplicationIcon(packagename);
答案 1 :(得分:1)
Drawable icon = getPackageManager().getApplicationIcon(packageInfo.packageName);
答案 2 :(得分:0)
使用此代码
getPackageManager().getApplicationIcon(packagename);
答案 3 :(得分:0)
public static Drawable getApplicationIcon(Context context, String packname){
PackageManager pm = context.getPackageManager();
try {
PackageInfo pInfo = pm.getPackageInfo(packname, 0);
int resId = pInfo.applicationInfo.icon;
AssetManager assetManager = AssetManager.class.newInstance();
AssetManager.class.getMethod("addAssetPath", new Class[]{String.class}).invoke(assetManager, pInfo.applicationInfo.sourceDir);
Resources oRes = context.getResources();
Resources resources = new Resources(assetManager, oRes.getDisplayMetrics(), oRes.getConfiguration());
Drawable result = resources.getDrawable(resId);
assetManager.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return pm.getDefaultActivityIcon();
}
答案 4 :(得分:0)
很抱歉迟到的回复。但是我希望这会帮助新手安装。
此方法将获取与活动相关联的图标。
public static Drawable getOriginalActivityIcon(Context context,ResolveInfo resolveInfo){
PackageManager packageManager=context.getPackageManager();
try {
ActivityInfo info=resolveInfo.activityInfo;
Drawable drawable = packageManager.getDrawable(info.packageName,info.getIconResource(), packageManager.getApplicationInfo(info.packageName, 0));
if(drawable==null){
drawable=resolveInfo.loadIcon(packageManager);
}
return drawable;
}catch (Exception e){
e.printStackTrace();
}
return packageManager.getDefaultActivityIcon();
}