如何获取应用程序图标?

How to get the application icon?

我正在尝试使用所有已安装应用程序的名称、权限和图标来制作一个应用程序。 第一个和第二个都成功了,第三个就不知道怎么办了

这是 AppsAdapter class:

class AppsAdapter extends BaseAdapter {

private final Context mContext;
private List<Pair<String, List<String>>> mAppsWithPermission;

public AppsAdapter(Context context, List<Pair<String, List<String>>> appsWithPermission) {
    mContext = context;
    mAppsWithPermission = appsWithPermission;
}

static class ViewHolder {
    public TextView appName;
    public TextView appPermissions;
    public ImageView appIcon;
}

@Override
public int getCount() {
    return mAppsWithPermission.size();
}

@Override
public Object getItem(int position) {
    return mAppsWithPermission.get(position);
}

@Override
public long getItemId(int position) {
    return mAppsWithPermission.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {

        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.appName = convertView.findViewById(R.id.list_item_appname);
        holder.appPermissions = convertView.findViewById(R.id.list_item_apppermissions);
        holder.appIcon = convertView.findViewById(R.id.list_item_appicon);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Pair<String, List<String>> item = mAppsWithPermission.get(position);

    holder.appName.setText(item.first);
    holder.appPermissions.setText(item.second.toString());


    return convertView;

}
}

主要活动:

public class MainActivity extends AppCompatActivity{


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

    final PackageManager packageManager = getPackageManager();

    final ApplicationInfo packageNames = new ApplicationInfo();

    final List<Pair<String, List<String>>> appsWithPermission = new ArrayList<>();

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
    for (ResolveInfo info : apps) {
        final ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
        final String appName = (String) applicationInfo.loadLabel(packageManager);

        final List<String> permissions = new ArrayList<>();

        if (appName != null) {

            try {
                final PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
                final String[] requestedPermissions = packageInfo.requestedPermissions;

                if (requestedPermissions != null) {
                    permissions.addAll(Arrays.asList(requestedPermissions));
                }

            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }

        appsWithPermission.add(new Pair<>(appName, permissions));
    }

    final ListView listView = findViewById(R.id.list_view);
    final AppsAdapter appsAdapter = new AppsAdapter(this, appsWithPermission);
    listView.setAdapter(appsAdapter);

}
}

我也上传了更新后的 MainActivity。

我正在尝试获取应用程序图标。

我该怎么办?

提前致谢

法比

我建议重构您的适配器以容纳 List<PackageItemInfo>。在您应用程序的某处调用 PackageManager.getInstalledPackages 返回 PackageItemInfo 对象列表 - 将其传递给您的适配器。然后使用PackageItemInfo.loadIcon加载相应的图标到你的ImageView.

试试这个

try {
      String pkg = "com.simple.app";//your package name
      Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
     imageView.setImageDrawable(icon);
   } catch (PackageManager.NameNotFoundException ne) {
}