在Android中获取App图标

时间:2011-01-05 04:07:36

标签: android

我在检索安装的Android应用程序的图标时遇到了一些问题。以下代码返回所有应用程序的图标,但是,某些应用程序返回带有小图标(可能来自drawable - ldpi文件夹),而某些应用程序带有大图标。

List<PackageInfo> package = getPackageManager().getInstalledPackages(0);

for(PackageInfo p : package){
    iApp.icon = p.applicationInfo.loadIcon(getPackageManager()); 
}

如何获取大小相同的图标,就像在Android的“管理应用程序”部分中一样?

4 个答案:

答案 0 :(得分:20)

应用可能只有不同大小的图标。 Android设置应用加载与您一样的图标。它在显示时会缩放图标。

以下是来自packages / apps / Settings / res / layout / manage_applications_item.xml的代码段:

<ImageView android:id="@+id/app_icon"
    android:layout_width="@android:dimen/app_icon_size"
    android:layout_height="@android:dimen/app_icon_size"
    android:layout_marginRight="11dip"
    android:layout_gravity="center_vertical"
    android:scaleType="fitCenter"/>

答案 1 :(得分:19)

我想在之前的回答中添加适用于Android 3.0及更高版本的内容。

您可以注意到以此标准方式检索的图标(我的意思是applicationInfo.loadIcon和其他典型方法)小于您在启动器应用中看到的图标。向上扩展只会使图标变得模糊。如果你想要大图标,你可以使用下一个代码(我从启动器源代码中获取它,你可以找到here并稍微改变一下)。请注意activityManager.getLauncherLargeIconDensity方法。

public Drawable getFullResDefaultActivityIcon() {
    return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
}

public Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        int iconDpi = activityManager.getLauncherLargeIconDensity();
        d = resources.getDrawableForDensity(iconId, iconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }

    return (d != null) ? d : getFullResDefaultActivityIcon();
}

public Drawable getFullResIcon(String packageName, int iconId) {
    Resources resources;
    try {
        resources = mContext.getPackageManager().getResourcesForApplication(packageName);
    } catch (PackageManager.NameNotFoundException e) {
        resources = null;
    }
    if (resources != null) {
        if (iconId != 0) {
            return getFullResIcon(resources, iconId);
        }
    }
    return getFullResDefaultActivityIcon();
}

public Drawable getFullResIcon(ResolveInfo info) {
    return getFullResIcon(info.activityInfo);
}

public Drawable getFullResIcon(ActivityInfo info) {
    Resources resources;
    try {
        resources = mContext.getPackageManager().getResourcesForApplication(info.applicationInfo);
    } catch (PackageManager.NameNotFoundException e) {
        resources = null;
    }
    if (resources != null) {
        int iconId = info.getIconResource();
        if (iconId != 0) {
            return getFullResIcon(resources, iconId);
        }
    }
    return getFullResDefaultActivityIcon();
}

private Drawable getAppIcon(ResolveInfo info) {
    return getFullResIcon(info.activityInfo);
}

希望它有所帮助。

答案 2 :(得分:6)

虽然这已经回答了很久,但这是完整的功能。希望它能帮助别人。

要获取图标,您应该查询不同dpi的图标。

试试这个:

public static Drawable getIconFromPackageName(String packageName, Context context)
    {
        PackageManager pm = context.getPackageManager();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
        {
            try
            {
                PackageInfo pi = pm.getPackageInfo(packageName, 0);
                Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

                int displayMetrics[] = {DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_TV};

                for (int displayMetric : displayMetrics)
                {
                    try
                    {
                        Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon, displayMetric);
                        if (d != null)
                        {
                            return d;
                        }
                    }
                    catch (Resources.NotFoundException e)
                    {
//                      Log.d(TAG, "NameNotFound for" + packageName + " @ density: " + displayMetric);
                        continue;
                    }
                }

            }
            catch (Exception e)
            {
                // Handle Error here
            }
        }

        ApplicationInfo appInfo = null;
        try
        {
            appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        }
        catch (PackageManager.NameNotFoundException e)
        {
            return null;
        }

        return appInfo.loadIcon(pm);
    }

答案 3 :(得分:0)

您必须在App类中定义;

private Drawable appIcon; 

public Drawable getappIcon(){
        return this.appIcon;
    }

如果您使用RecyclerViewAdapter;

   List<App> list_app;

    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.appIcon.setImageDrawable(list_app.get(position).getappIcon());

    }

在MainActivity中;

private List<App> appList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final PackageManager pm = getPackageManager();
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

        appList = new ArrayList<App>();

        for (ApplicationInfo packageInfo : packages) {
          Drawable drawableIcon =packageInfo.loadIcon(getPackageManager());
          appList.add(new App (drawableIcon));

        }