Как можно взять иконки чужих приложений?

Я пишу программу на Android и в неё входит создание списка со всеми приложениями телефона с иконками этих приложений, и я пришел к такой проблеме "Как из телефона взять иконки чужих приложений?"

Вот часть кода которая берет названия приложений и записывает их в список rowbuttonlayout в TextView lable:

public class MainActivity extends ListActivity {

    public final ArrayList results = new ArrayList();

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        PackageManager pm = this.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        @SuppressLint({"QueryPermissionsNeeded", "WrongConstant"}) List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo : list) {
            results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.rowbuttonlayout, R.id.label, results);
        setListAdapter(adapter);

    }
}

rowbuttonlayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_toEndOf="@id/icon"
        android:layout_alignBottom="@id/icon"
        android:textSize="20sp" >
    </TextView>

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="10dp"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@id/label">
    </CheckBox>
</RelativeLayout>

Ответы (1 шт):

Автор решения: kitfist0

Посе того как вы получили список имен пакетов, установленных на телефоне приложений, вы можете получить иконки этих приложений. Метод ниже позволит вам по имени пакета получить иконку приложения:

private Drawable getAppIcon(Context context, String packageName) {
    // default_icon – некоторая иконка, которая будет отображаться
    // по умолчанию, если getApplicationIcon() выбросит Exception
    Drawable appIcon = ContextCompat.getDrawable(context, R.drawable.default_icon);
    try {
        appIcon = context.getPackageManager().getApplicationIcon(packageName);
    } catch (Exception ignore) {
    }
    return appIcon;
}
→ Ссылка