Stack Overflow archive
5 score

Use Picasso to load installed apps icon

score
5
question views
1.1K
license
CC BY-SA 4.0

I know this is old, but I thought I would share my solution for anyone else who comes across this. You can use a custom RequestHandler. Here is a RequestHandler I wrote for loading app icons using Picasso:

java
public class AppIconRequestHandler extends RequestHandler {

  public static final String SCHEME_PNAME = "pname";

  private final PackageManager pm;
  private final int dpi;
  private Bitmap defaultAppIcon;

  public AppIconRequestHandler(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    dpi = am.getLauncherLargeIconDensity();
    pm = context.getPackageManager();
  }

  @Override public boolean canHandleRequest(Request data) {
    return data.uri != null && TextUtils.equals(data.uri.getScheme(), SCHEME_PNAME);
  }

  @Override public Result load(Request request, int networkPolicy) throws IOException {
    try {
      return new Result(getFullResIcon(request.uri.toString().split(":")[1]), DISK);
    } catch (PackageManager.NameNotFoundException e) {
      return null;
    }
  }

  private Bitmap getFullResIcon(String packageName) throws PackageManager.NameNotFoundException {
    return getFullResIcon(pm.getApplicationInfo(packageName, 0));
  }

  private Bitmap getFullResIcon(ApplicationInfo info) {
    try {
      Resources resources = pm.getResourcesForApplication(info.packageName);
      if (resources != null) {
        int iconId = info.icon;
        if (iconId != 0) {
          return getFullResIcon(resources, iconId);
        }
      }
    } catch (PackageManager.NameNotFoundException ignored) {
    }
    return getFullResDefaultActivityIcon();
  }

  private Bitmap getFullResIcon(Resources resources, int iconId) {
    final Drawable drawable;
    try {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        drawable = resources.getDrawableForDensity(iconId, dpi, null);
      } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        drawable = resources.getDrawableForDensity(iconId, dpi);
      } else {
        drawable = resources.getDrawable(iconId);
      }
    } catch (Resources.NotFoundException e) {
      return getFullResDefaultActivityIcon();
    }
    return drawableToBitmap(drawable);
  }

  private Bitmap getFullResDefaultActivityIcon() {
    if (defaultAppIcon == null) {
      Drawable drawable;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        drawable = Resources.getSystem().getDrawableForDensity(
            android.R.mipmap.sym_def_app_icon, dpi);
      } else {
        drawable = Resources.getSystem().getDrawable(
            android.R.drawable.sym_def_app_icon);
      }
      defaultAppIcon = drawableToBitmap(drawable);
    }
    return defaultAppIcon;
  }

}

Usage

Simply add the RequestHandler to your Picasso instance:

java
Picasso.setSingletonInstance(new Picasso.Builder(this)
    .addRequestHandler(new AppIconRequestHandler(this))
    .build());

Then, create a Uri with the scheme used in the RequestHandler:

java
Uri uri = Uri.parse(AppIconRequestHandler.SCHEME_PNAME +  ":" + "com.facebook.katana");
Picasso.with(context).load(uri).into(imageView);

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.