Android How to - Get APK Icon








Question

We would like to know how to get APK Icon.

Answer

//  w  w  w  .  j a  v a  2  s .c  om

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

public class Main {
    
    public static Drawable getApkIcon3(Context context,String apkPath) {

        PackageManager pm = context.getPackageManager();

        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            appInfo.publicSourceDir = apkPath;
            Drawable icon = appInfo.loadIcon(pm);
            return icon;
        }
        
        return null;
    }

    public static Drawable getApkIcon2(Context context, String apkPath) {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            appInfo.publicSourceDir = apkPath;
            Drawable icon = appInfo.loadIcon(pm);
            return icon;
        }
        return null;
    }

    public static Drawable test(Context context, String apkPath) {

        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath,
                PackageManager.GET_ACTIVITIES);

        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            String appName = pm.getApplicationLabel(appInfo).toString();
            String packageName = appInfo.packageName; 
            String version = info.versionName;
            Toast.makeText(
                    context,
                    "packageName:" + packageName + ";version:" + version
                            + "name:" + appName, Toast.LENGTH_LONG).show();
            appInfo.publicSourceDir = apkPath;
            Drawable icon = pm.getApplicationIcon(appInfo);
            return icon;
        }
        return null;
    }
}