Example usage for android.content.pm PackageManager getApplicationIcon

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

Introduction

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

Prototype

public abstract Drawable getApplicationIcon(String packageName) throws NameNotFoundException;

Source Link

Document

Retrieve the icon associated with an application.

Usage

From source file:Main.java

public static Drawable getAppIconByPkgName(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    Drawable drawable = null;/*from w  w  w. j  av  a  2  s . c om*/
    try {
        drawable = pm.getApplicationIcon(packageName);
    } catch (PackageManager.NameNotFoundException mE) {
        mE.printStackTrace();
    }
    return drawable;
}

From source file:Main.java

public static Drawable appIconLookup(Context context, String packageName) {

    try {/*from   w w  w .  j a  va 2  s .com*/

        PackageManager pm = context.getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        return pm.getApplicationIcon(ai);

    } catch (final PackageManager.NameNotFoundException e) {

        e.printStackTrace();
        Log.e("AppsHelper", "Could not find icon for " + packageName);

        return null;

    }

}

From source file:Main.java

public static Drawable getAppIcon(Context context, String apkFilePath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo packageInfo = getPackageInfo(context, apkFilePath);
    if (packageInfo == null)
        return null;
    ApplicationInfo applicationInfo = packageInfo.applicationInfo;
    applicationInfo.sourceDir = apkFilePath;
    applicationInfo.publicSourceDir = apkFilePath;
    return pm.getApplicationIcon(applicationInfo);
}

From source file:Main.java

public static Drawable getAppIcon(Context context, String apkFilepath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pkgInfo = getPackageInfo(context, apkFilepath);
    if (pkgInfo == null) {
        return null;
    }//from  ww w.  j av  a2 s. c  o m

    // Workaround for http://code.google.com/p/android/issues/detail?id=9151
    ApplicationInfo appInfo = pkgInfo.applicationInfo;
    if (Build.VERSION.SDK_INT >= 8) {
        appInfo.sourceDir = apkFilepath;
        appInfo.publicSourceDir = apkFilepath;
    }

    return pm.getApplicationIcon(appInfo);
}

From source file:Main.java

public static Bitmap packageNameToBitmap(Context context, PackageManager packageManager, String packageName,
        int resId) {
    try {/*from w  w  w.  j  a va 2 s .  co  m*/
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,
                PackageManager.GET_META_DATA);
        Resources resources = packageManager.getResourcesForApplication(applicationInfo);
        Bitmap bitmap = resIdToBitmap(resources, resId);
        if (bitmap == null) {
            Drawable drawable = packageManager.getApplicationIcon(packageName);
            if (drawable != null) {
                bitmap = drawableToBitmap(drawable);
            }
        }
        return bitmap;
    } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * ??????.//from  w w  w.ja v a  2 s .  co  m
 *
 * @param context     
 * @param packageName ???
 * @param iconId      ?ID
 * @return ??
 */
public static Drawable loadPluginIcon(final Context context, final String packageName, final Integer iconId) {
    PackageManager pkgMgr = context.getPackageManager();
    Drawable icon;
    if (iconId != null) {
        icon = ResourcesCompat.getDrawable(context.getResources(), iconId, null);
    } else {
        try {
            ApplicationInfo info = pkgMgr.getApplicationInfo(packageName, 0);
            icon = pkgMgr.getApplicationIcon(info.packageName);
        } catch (PackageManager.NameNotFoundException e) {
            icon = null;
            if (BuildConfig.DEBUG) {
                Log.d("Manager", "Icon is not found.");
            }
        }
    }
    return icon;
}

From source file:com.jungle.base.utils.MiscUtils.java

public static Drawable getAppIcon() {
    String pkgName = MiscUtils.getPackageName();
    PackageManager mgr = BaseApplication.getAppContext().getPackageManager();

    try {//from   ww  w.j a  va2 s .c o  m
        ApplicationInfo info = mgr.getApplicationInfo(pkgName, 0);
        return mgr.getApplicationIcon(info);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.noshufou.android.su.util.Util.java

public static Drawable getAppIcon(Context c, int uid) {
    PackageManager pm = c.getPackageManager();
    Drawable appIcon = c.getResources().getDrawable(R.drawable.sym_def_app_icon);
    String[] packages = pm.getPackagesForUid(uid);

    if (packages != null) {
        //            if (packages.length == 1) {
        try {/*from w  ww.  j a  v  a  2 s  . co  m*/
            ApplicationInfo appInfo = pm.getApplicationInfo(packages[0], 0);
            appIcon = pm.getApplicationIcon(appInfo);
        } catch (NameNotFoundException e) {
            Log.e(TAG, "No package found matching with the uid " + uid);
        }
        //            }
    } else {
        Log.e(TAG, "Package not found for uid " + uid);
    }

    return appIcon;
}

From source file:org.sufficientlysecure.keychain.remote.ui.AppSettingsFragment.java

private void updateView(AppSettings appSettings) {
    // get application name and icon from package manager
    String appName;/*from  w w w .ja  va2  s.c om*/
    Drawable appIcon = null;
    PackageManager pm = getActivity().getApplicationContext().getPackageManager();
    try {
        ApplicationInfo ai = pm.getApplicationInfo(appSettings.getPackageName(), 0);

        appName = (String) pm.getApplicationLabel(ai);
        appIcon = pm.getApplicationIcon(ai);
    } catch (NameNotFoundException e) {
        // fallback
        appName = appSettings.getPackageName();
    }
    mAppNameView.setText(appName);
    mAppIconView.setImageDrawable(appIcon);

    // advanced info: package name
    mPackageName.setText(appSettings.getPackageName());

    // advanced info: package signature SHA-256
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(appSettings.getPackageSignature());
        byte[] digest = md.digest();
        String signature = new String(Hex.encode(digest));

        mPackageSignature.setText(signature);
    } catch (NoSuchAlgorithmException e) {
        Log.e(Constants.TAG, "Should not happen!", e);
    }
}

From source file:org.sufficientlysecure.keychain.remote.ui.AppSettingsHeaderFragment.java

private void updateView(AppSettings appSettings) {
    // get application name and icon from package manager
    String appName;//  ww w.j  a v a 2 s  .  c  o m
    Drawable appIcon = null;
    PackageManager pm = getActivity().getApplicationContext().getPackageManager();
    try {
        ApplicationInfo ai = pm.getApplicationInfo(appSettings.getPackageName(), 0);

        appName = (String) pm.getApplicationLabel(ai);
        appIcon = pm.getApplicationIcon(ai);
    } catch (NameNotFoundException e) {
        // fallback
        appName = appSettings.getPackageName();
    }
    mAppNameView.setText(appName);
    mAppIconView.setImageDrawable(appIcon);

    // advanced info: package name
    mPackageName.setText(appSettings.getPackageName());

    // advanced info: package signature SHA-256
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(appSettings.getPackageCertificate());
        byte[] digest = md.digest();
        String signature = new String(Hex.encode(digest));

        mPackageCertificate.setText(signature);
    } catch (NoSuchAlgorithmException e) {
        Log.e(Constants.TAG, "Should not happen!", e);
    }
}