Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving xhdpi app icons from packageManager?

I have an activity in my app wherein a ListView of all installed apps is generated. In addition to the app name, the app icon appears as well. I had created an array of objects containing all the information about the application, among this the icon, and retrieved the icon using:

drawableAppIcon = mPackageManager.getApplicationIcon(apps.applicationInfo);

Where application is a <PackageInfo> object. The problem with this is that while I readily have access to all the drawables, various apps seem to pull various quality drawables; one app icon will be extremely high res while another will be the low res version. I would have thought that the above line of code would have accounted for screen size and found the appropriate icons, but it doesn't. Is there an alternative that accounts for screen size? Or even more preferably is there a way to extract just the highest res icon and then scale it down based on screen size, which would basically guarantee all of them look clean?

EDIT: I've found a solution, but it only works for Android 4.0+

try {
    Context otherAppCtxt = context.createPackageContext(apps.applicationInfo.packageName, Context.CONTEXT_IGNORE_SECURITY);
    info.drawableAppIcon = otherAppCtxt.getResources().getDrawableForDensity(apps.applicationInfo.icon, DisplayMetrics.DENSITY_XHIGH);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

For others' reference, this works, if someone has a solution for older version of Android that would be really helpful.

like image 997
mike Avatar asked Aug 22 '13 16:08

mike


2 Answers

ApplicationInfo applicationInfo = 
        packagemanager.getApplicationInfo(packageName(), PackageManager.GET_META_DATA);
Resources res = packagemanager.getResourcesForApplication(applicationInfo);
Drawable appIcon = res.getDrawableForDensity(applicationInfo.icon, 
                                             DisplayMetrics.DENSITY_XXXHIGH, 
                                             null);
like image 54
sorcererxw Avatar answered Sep 18 '22 12:09

sorcererxw


I suspect there is some other reason why the icons look bad (such as resizing within your app), but if you need to do it for any Android version:

// Get the application's resources
Resources res = mPackageManager.getResourcesForApplication(appInfo);

// Get a copy of the configuration, and set it to the desired resolution
Configuration config = res.getConfiguration();
Configuration originalConfig = new Configuration(config);
config.densityDpi = DisplayMetrics.DENSITY_XHIGH;

// Update the configuration with the desired resolution
DisplayMetrics dm = res.getDisplayMetrics();
res.updateConfiguration(config, dm);

// Grab the app icon
Drawable appIcon = res.getDrawable(appInfo.icon);

// Set our configuration back to what it was
res.updateConfiguration(originalConfig, dm);
like image 28
Dane White Avatar answered Sep 20 '22 12:09

Dane White