How do I get the original app logo badge?

The application icon can be obtained by the following code:

Drawable drawable = ResolveInfo.loadIcon(packageManager);

      

But it is possible that the ROM can be changed (for example: the right corner is changed to the corner of the corner corner), so my question is how can I get the original logo from the program?

PS some launchers can do it like MIUI launcher.

+3


source to share


4 answers


You can get the app icon using PackageManager



PackageManager manager = context.getPackageManager();
Drawable appIcon = manager.getApplicationIcon(packagename);

      

+3


source


Use this code



getPackageManager().getApplicationIcon(packagename);

      

0


source


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();
}

      

0


source


Sorry for the late reply. But I hope this helps someone new to Android.

This method will get the icon associated with the Activity.

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();
}

      

0


source







All Articles