Last active
September 6, 2016 03:41
-
-
Save hyongbai/e07f13766cfd519ac9bad45c3440d2ca to your computer and use it in GitHub Desktop.
get full res icon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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