Skip to content

Instantly share code, notes, and snippets.

@hyongbai
Last active September 6, 2016 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyongbai/e07f13766cfd519ac9bad45c3440d2ca to your computer and use it in GitHub Desktop.
Save hyongbai/e07f13766cfd519ac9bad45c3440d2ca to your computer and use it in GitHub Desktop.
get full res icon
```Java
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;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment