4

Application's icon can be gotten by following code:

Drawable drawable = ResolveInfo.loadIcon(packageManager);

But it is the drawable that maybe modified by ROM (like: right-angle changed to round angle corner), so my question is how can I get original logo icon by program?

P.S. some launcher can do this, like MIUI launcher.

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
RoFF
  • 529
  • 5
  • 27

6 Answers6

3

You can get application icon using PackageManager

PackageManager manager = context.getPackageManager();
Drawable appIcon = manager.getApplicationIcon(packagename);
santoshavss
  • 221
  • 1
  • 6
1
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();
}
Will Li
  • 495
  • 1
  • 3
  • 12
1

Sorry for the late reply. But I hope this will help 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

Use this code

getPackageManager().getApplicationIcon(packagename);
QArea
  • 4,955
  • 1
  • 12
  • 22
0
Drawable icon = getPackageManager().getApplicationIcon(packageInfo.packageName);
Tony
  • 259
  • 3
  • 13
0

The conclusion is that if a round icon exists and no adaptive icon exists, you can't get a square icon. Unless this system is customized with a different Android framework than AOSP, such as the MIUI.

At first I was just like you, looking around for a way to load the square icon. I've tried every answer including the ones here.
Finally I found that if both square and round icons are set, then the Android system framework returns the ResId of the round icon when you request the ResId of the icon. At last I went searching for the code of the Android system framework and verified this guess:

When a developer specifies an adaptive application icon, and a non-adaptive round application icon, create an alias from the round icon to the regular icon for v26 APIs and up. We do this because certain devices prefer android:roundIcon over android:icon regardless of the API levels of the drawables set for either. This auto-aliasing behaviour allows an app to prefer the android:roundIcon on API 25 devices, and prefer the adaptive icon on API 26 devices.

If the application contains adaptive icons and the API level >= 26, you can use it to draw out square icon. For example.