How to find high resoultion icon using PacakageManager

1.1k views Asked by At

I'm doing an app that need the higher resolution icons of each app.

I have done some research since this morning but none of the answers I found work.

Here are two of them and what is wrong for me.

FYI, I have retrieved all the "packageinfo" from the packagemanager, I'm finding everything I need but the high resolution icon...

1. using getDrawableForDensity:

ActivityInfo info = packageManager.getActivityInfo(componentName,     PackageManager.GET_META_DATA);

Resources resources;
try {
    resources = mPackageManager.getResourcesForApplication(
        info.applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}

if (resources != null) {
int iconId = info.getIconResource();
if (iconId != 0) {
    Drawable d;
    try {
        d = resources.getDrawableForDensity(iconId, DisplayMetrics.DENSITY_XHIGH);
    } catch (Resources.NotFoundException e) {
        d = null;
    }
    return d;
}
}

My problem for this one is that I don't understand what to put for componentName. How can I "construct" it with the information I got from packagemanager ?

2.Other solution Retrieving xhdpi app icons from packageManager?

// 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);

for this one I have a problem with this line:

Drawable appIcon = res.getDrawable(appInfo.icon);

Error I get about "icon":

icon cannot be resolved or is not a field

2

There are 2 answers

0
Ushal Naidoo On BEST ANSWER

Try this, it is similar to the code that you have but it seems to work for me :

//Get HiRes Icon

             Drawable appIcon;
List<Application> applications = new ArrayList<Application>();
         for (ApplicationInfo item : getInstalledApplications(packageManager)) {

             try {
               Resources resourcesForApplication = packageManager.getResourcesForApplication(item);
               Configuration config = resourcesForApplication.getConfiguration();
               Configuration originalConfig = new Configuration(config);

               DisplayMetrics displayMetrics = resourcesForApplication.getDisplayMetrics();
               DisplayMetrics originalDisplayMetrics =  resourcesForApplication.getDisplayMetrics();
               displayMetrics.densityDpi =  DisplayMetrics.DENSITY_DEFAULT;
               resourcesForApplication.updateConfiguration(config, displayMetrics);

               appIcon = resourcesForApplication.getDrawable(item.icon);
               resourcesForApplication.updateConfiguration(originalConfig, originalDisplayMetrics);
             } catch (PackageManager.NameNotFoundException e) {
               Log.e("check", "error getting Hi Res Icon :", e);
               appIcon = item.loadIcon(packageManager);
             }

             Application app = new Application();
             app.setTitle(item.loadLabel(packageManager).toString());
             app.setPackageName(item.packageName);
             // app.setImage(item.loadIcon(packageManager));
             app.setImage(appIcon);
             applications.add(app);
           }
    }
1
Molter On

This is only speculation as I'm not a particularly good Android programmer but: In the first solution, you could probably retrieve a list of every application installed on a device by a method getInstalledApplications(int flags) of PackageManager class. See http://developer.android.com/reference/android/content/pm/PackageManager.html#getInstalledApplications(int)

So as a code something like this:

///This retrieves information on every application installed on device, see documentation 
///for flags
List<ApplicationInfo> applications = mPackageManager.getInstalledApplications(int flags);
///Then retrieve Resources for every application with looping through all the applications
for (ApplicationInfo info : applications) {
Resources res = mPackageManager.getResourcesForApplication(info);
///And now you can do what you already wrote yourself and try fetching icon for every
///'res' you did in the first solution.
}

This is assuming that you want to show every application.

About solution 2: I'm assuming appInfo is of 'ApplicationInfo' class. It has a public field 'icon' that is inherited from the 'PackageItemInfo'-class. Make sure your appInfo is any of the sub classes of 'PackageItemInfo' class and it should work. If it does not work, there is probably something wrong with the Android manifest.xml of the given application and it can not retrieve the required integer.

You could try retrieving the icons by 'getApplicationIcon'-method of the 'PackageManager'-class. Once again

List<ApplicationInfo> applications = mPackageManager.getInstalledApplications(int flags);
List<Drawable> icons = new List<Drawable>;
for (ApplicationInfo app : applications) {
    icons.add(mPackageManager.getApplicationIcon(app));
}

Of course, this means you are going twice through the list of applications, short of.