Example usage for android.content.pm PackageManager getDefaultActivityIcon

List of usage examples for android.content.pm PackageManager getDefaultActivityIcon

Introduction

In this page you can find the example usage for android.content.pm PackageManager getDefaultActivityIcon.

Prototype

public abstract Drawable getDefaultActivityIcon();

Source Link

Document

Return the generic icon for an activity that is used when no specific icon is defined.

Usage

From source file:Main.java

public static Drawable loadIcon(Context context, int icon, String packageName) {
    PackageManager pm = context.getPackageManager();
    if (icon != 0) {
        Drawable dr = pm.getDrawable(packageName, icon, null);
        if (dr != null) {
            return dr;
        }//from   w  w  w.j a  va 2s . c  om
    }
    return pm.getDefaultActivityIcon();
}

From source file:org.mozilla.gecko.GeckoAppShell.java

public static byte[] getIconForExtension(String aExt, int iconSize) {
    try {//from   ww w. j a va2 s .c  o  m
        if (iconSize <= 0)
            iconSize = 16;

        if (aExt != null && aExt.length() > 1 && aExt.charAt(0) == '.')
            aExt = aExt.substring(1);

        PackageManager pm = GeckoApp.mAppContext.getPackageManager();
        Drawable icon = getDrawableForExtension(pm, aExt);
        if (icon == null) {
            // Use a generic icon
            icon = pm.getDefaultActivityIcon();
        }

        Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
        if (bitmap.getWidth() != iconSize || bitmap.getHeight() != iconSize)
            bitmap = Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, true);

        ByteBuffer buf = ByteBuffer.allocate(iconSize * iconSize * 4);
        bitmap.copyPixelsToBuffer(buf);

        return buf.array();
    } catch (Exception e) {
        Log.i(LOGTAG, "getIconForExtension error: ", e);
        return null;
    }
}