We can achieve by following way,
AppIconHelper.java
public class AppIconHelper {
public static Bitmap getAppIcon(PackageManager mPackageManager, String packageName) {
if (Build.VERSION.SDK_INT >= 26) {
return AppIconHelperV26.getAppIcon(mPackageManager, packageName);
}
try {
Drawable drawable = mPackageManager.getApplicationIcon(packageName);
return ((BitmapDrawable) drawable).getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
For device supporting Android O( >=26) we can call AppIconHelperV26 class to get the AppIcon, In this, if its drawable is AdaptiveIconDrawable, we are doing the following steps
1. Get the Foreground and Background drawable and create as Layer Drawable
2. Using Canvas we can convert the Layer drawable to Bitmap
AppIconHelperV26.java
public class AppIconHelperV26 {
@RequiresApi(api = Build.VERSION_CODES.O)
public static Bitmap getAppIcon(PackageManager mPackageManager, String packageName) {
try {
Drawable drawable = mPackageManager.getApplicationIcon(packageName);
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof AdaptiveIconDrawable) {
Drawable backgroundDr = ((AdaptiveIconDrawable) drawable).getBackground();
Drawable foregroundDr = ((AdaptiveIconDrawable) drawable).getForeground();
Drawable[] drr = new Drawable[2];
drr[0] = backgroundDr;
drr[1] = foregroundDr;
LayerDrawable layerDrawable = new LayerDrawable(drr);
int width = layerDrawable.getIntrinsicWidth();
int height = layerDrawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layerDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
layerDrawable.draw(canvas);
return bitmap;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
Now you can call AppIconHelper.getAppIcon to get the Bitmap,
AppListAdapter.java
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//... your code here
Bitmap appIcon = AppIconHelper.getAppIcon(packageManager, packageName);
imageView.setImageBitmap(appIcon);
}