Example usage for android.content.pm PackageManager getDrawable

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

Introduction

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

Prototype

public abstract Drawable getDrawable(String packageName, @DrawableRes int resid, ApplicationInfo appInfo);

Source Link

Document

Retrieve an image from a package.

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  v a  2  s . c  o  m*/
    }
    return pm.getDefaultActivityIcon();
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

/**
 * Gets the activity or application icon for an activity.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the acitivy or the application
 *         have an icon set.//from  w w  w .j  ava  2 s .  com
 */
private Drawable getActivityIcon(ComponentName component) {
    PackageManager pm = mContext.getPackageManager();
    final ActivityInfo activityInfo;
    try {
        activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException ex) {
        Log.w(LOG_TAG, ex.toString());
        return null;
    }
    int iconId = activityInfo.getIconResource();
    if (iconId == 0)
        return null;
    String pkg = component.getPackageName();
    Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
    if (drawable == null) {
        Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " + component.flattenToShortString());
        return null;
    }
    return drawable;
}

From source file:de.vanita5.twittnuker.util.Utils.java

private static Drawable getMetadataDrawable(final PackageManager pm, final ActivityInfo info,
        final String key) {
    if (pm == null || info == null || info.metaData == null || key == null || !info.metaData.containsKey(key))
        return null;
    final Drawable d = pm.getDrawable(info.packageName, info.metaData.getInt(key), info.applicationInfo);
    return d;//from  w w w . java 2s.c o  m
}

From source file:org.getlantern.firetweet.util.Utils.java

private static Drawable getMetadataDrawable(final PackageManager pm, final ActivityInfo info,
        final String key) {
    if (pm == null || info == null || info.metaData == null || key == null || !info.metaData.containsKey(key))
        return null;
    return pm.getDrawable(info.packageName, info.metaData.getInt(key), info.applicationInfo);
}