Example usage for android.content.pm ApplicationInfo loadIcon

List of usage examples for android.content.pm ApplicationInfo loadIcon

Introduction

In this page you can find the example usage for android.content.pm ApplicationInfo loadIcon.

Prototype

public Drawable loadIcon(PackageManager pm) 

Source Link

Document

Retrieve the current graphical icon associated with this item.

Usage

From source file:Main.java

public static Drawable getApkIcon(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        info.applicationInfo.sourceDir = apkPath;
        info.applicationInfo.publicSourceDir = apkPath;
        ApplicationInfo applicationInfo = info.applicationInfo;
        return applicationInfo.loadIcon(pm);
    }/* w ww . ja va  2 s .  c  o m*/
    return null;
}

From source file:Main.java

/**
 * Get app icon/*from   w w  w  .  java 2 s  . com*/
 *
 * @param context
 * @param packageName
 * @return
 */
public static Drawable getAppIcon(Context context, String packageName) {
    try {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
        return info.loadIcon(pm);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap getApkIcon(Context context, String apkPath) {
    Bitmap thumb = null;/*from  w w w  .j  a v  a  2  s .  com*/
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            thumb = ((BitmapDrawable) appInfo.loadIcon(pm)).getBitmap();
        } catch (OutOfMemoryError e) {

        }
    }
    return thumb;
}

From source file:Main.java

@Nullable
public static Bitmap extractApkIcon(@NonNull final PackageManager pm, @NonNull final File file) {
    final String filePath = file.getPath();
    final PackageInfo packageInfo = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
    if (packageInfo != null) {
        final ApplicationInfo appInfo = packageInfo.applicationInfo;
        if (appInfo != null) {
            appInfo.sourceDir = filePath;
            appInfo.publicSourceDir = filePath;
            final Drawable icon = appInfo.loadIcon(pm);
            if (icon != null) {
                return ((BitmapDrawable) icon).getBitmap();
            }//ww w .j  a  v  a2  s  .  co m
        }
    }
    return null;
}

From source file:Main.java

public static Drawable getIconFromPackageName(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        try {/*from  ww w  .  jav a 2 s  . c  om*/
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

            int displayMetrics[] = { DisplayMetrics.DENSITY_XXXHIGH, DisplayMetrics.DENSITY_XXHIGH,
                    DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_MEDIUM,
                    DisplayMetrics.DENSITY_TV };

            for (int displayMetric : displayMetrics) {
                try {
                    Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon,
                            displayMetric);
                    if (d != null) {

                        return d;
                    }
                } catch (Resources.NotFoundException e) {
                }
            }

        } catch (Exception e) {
            // Handle Error here
        }
    }

    ApplicationInfo appInfo = null;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    return appInfo.loadIcon(pm);
}

From source file:com.lovejoy777sarootool.rootool.preview.IconPreview.java

private static Bitmap getPreview(File file) {
    final boolean isImage = MimeTypes.isPicture(file);
    final boolean isVideo = MimeTypes.isVideo(file);
    final boolean isApk = file.getName().endsWith(".apk");
    Bitmap mBitmap = null;//from   w  ww  .j  a v a2  s  . co  m
    String path = file.getAbsolutePath();

    if (isImage) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, o);
        o.inJustDecodeBounds = false;

        if (o.outWidth != -1 && o.outHeight != -1) {
            final int originalSize = (o.outHeight > o.outWidth) ? o.outWidth : o.outHeight;
            o.inSampleSize = originalSize / mWidth;
        }

        mBitmap = BitmapFactory.decodeFile(path, o);

        cache.put(path, mBitmap);
        return mBitmap;
    } else if (isVideo) {
        mBitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND);

        cache.put(path, mBitmap);
        return mBitmap;
    } else if (isApk) {
        final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);

        if (packageInfo != null) {
            final ApplicationInfo appInfo = packageInfo.applicationInfo;

            if (appInfo != null) {
                appInfo.sourceDir = path;
                appInfo.publicSourceDir = path;
                final Drawable icon = appInfo.loadIcon(pm);

                if (icon != null) {
                    mBitmap = ((BitmapDrawable) icon).getBitmap();
                }
            }
        } else {
            // load apk icon from /res/drawable/..
            mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.type_apk);
        }

        cache.put(path, mBitmap);
        return mBitmap;
    }
    return null;
}

From source file:com.xargsgrep.portknocker.asynctask.RetrieveApplicationsAsyncTask.java

@Override
protected List<Application> doInBackground(Void... params) {
    PackageManager packageManager = activity.getPackageManager();
    List<ApplicationInfo> installedApplications = packageManager
            .getInstalledApplications(PackageManager.GET_META_DATA);

    List<Application> applications = new ArrayList<Application>();
    for (ApplicationInfo applicationInfo : installedApplications) {
        if (isSystemPackage(applicationInfo)
                || packageManager.getLaunchIntentForPackage(applicationInfo.packageName) == null)
            continue;
        applications.add(new Application(packageManager.getApplicationLabel(applicationInfo).toString(),
                applicationInfo.loadIcon(packageManager), applicationInfo.packageName));
    }/* w w w  . j av  a 2  s.  c om*/

    Collections.sort(applications, new Comparator<Application>() {
        @Override
        public int compare(Application app1, Application app2) {
            return app1.getLabel().compareTo(app2.getLabel());
        }
    });
    applications.add(0, new Application("None", null, ""));

    return applications;
}

From source file:co.lemonlabs.android.slidingdebugmenu.SlidingDebugMenu.java

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    mContainer = (ViewGroup) findViewById(R.id.sdm__drawer_container);
    mDrawerSettings = findViewById(R.id.sdm__drawer_settings);
    mDrawerSubtitle = (TextView) findViewById(R.id.sdm__drawer_title_description);

    // Set drawer title icon and name
    final ApplicationInfo appInfo = getContext().getApplicationInfo();
    ((ImageView) findViewById(R.id.sdm__drawer_title_icon))
            .setImageDrawable(appInfo.loadIcon(getContext().getPackageManager()));
    ((TextView) findViewById(R.id.sdm__drawer_title)).setText(appInfo.labelRes);

    // Init drawer settings
    findViewById(R.id.sdm__settings_developer).setOnClickListener(this);
    findViewById(R.id.sdm__settings_battery).setOnClickListener(this);
    findViewById(R.id.sdm__settings_drawer).setOnClickListener(this);
    findViewById(R.id.sdm__settings_uninstall).setOnClickListener(this);
    findViewById(R.id.sdm__settings_app_info).setOnClickListener(this);
}

From source file:com.nagopy.android.mypkgs.model.loader.ApplicationIconLoader.java

/**
 * ???.<br>//from   w  w w .j av a 2 s.  co m
 * ?????????
 *
 * @param params ???????
 * @return ???????
 */
@Override
protected AppData doInBackground(AppData... params) {
    TextView textView = textViewWeakReference.get();
    if (textView == null) {
        return null;
    }
    AppData appData = params[0];
    PackageManager packageManager = context.getPackageManager();
    DebugUtil.verboseLog("doInBackground :" + appData.packageName);

    if (appData.icon != null && appData.icon.get() != null) {
        DebugUtil.verboseLog("use cache icon :" + appData.packageName);
        return appData;
    }

    try {
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(appData.packageName,
                RETRIEVE_FLAGS);
        Drawable icon;
        if (applicationInfo.icon == 0x0) {
            DebugUtil.verboseLog(appData.packageName + ", icon=0x0");
            icon = getDefaultIcon(context);
        } else {
            icon = applicationInfo.loadIcon(packageManager);
        }
        appData.icon = new WeakReference<>(icon);
        DebugUtil.verboseLog("load icon complete :" + appData.packageName);
        return appData;
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("ApplicationInfo??? packageName=" + appData.packageName, e);
    }
}

From source file:com.open.file.manager.IconLoader.java

/**
 * Get icon from the apk file//  w w w  .  j  a  va2s.  co m
 * @param current apk to load icon from
 * @return icon bitmap
 */
private Bitmap getApkIcon(File current) {
    try {
        Bitmap icon = null;
        PackageInfo packageInfo = mycont.getPackageManager().getPackageArchiveInfo(current.getAbsolutePath(),
                PackageManager.GET_ACTIVITIES);
        if (packageInfo != null) {
            ApplicationInfo appInfo = packageInfo.applicationInfo;
            if (Build.VERSION.SDK_INT >= 8) {
                appInfo.sourceDir = current.getAbsolutePath();
                appInfo.publicSourceDir = current.getAbsolutePath();
            }
            Drawable apkico = appInfo.loadIcon(mycont.getPackageManager());
            final float scale = mycont.getResources().getDisplayMetrics().density;
            final int targetHeight = Math.round(Consts.ICON_SIZE * scale);
            final int targetWidth = Math.round(Consts.ICON_SIZE * scale);
            icon = ((BitmapDrawable) apkico).getBitmap();
            icon = Bitmap.createScaledBitmap(icon, targetWidth, targetHeight, false);
        }
        return icon;
    } catch (Exception exc) {
        return null;
    }
}