2

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

4

2 回答 2

1

试试这个,它类似于您拥有的代码,但它似乎对我有用:

//获取 HiRes 图标

             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);
           }
    }
于 2014-05-13T01:54:39.237 回答
1

这只是推测,因为我不是一个特别好的 Android 程序员,但是:在第一个解决方案中,您可能可以通过 PackageManager 类的 getInstalledApplications(int flags) 方法检索设备上安装的每个应用程序的列表。请参阅 http://developer.android.com/reference/android/content/pm/PackageManager.html#getInstalledApplications(int)

所以作为这样的代码:

///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.
}

这是假设您要显示每个应用程序。

关于解决方案 2:我假设 appInfo 属于“ApplicationInfo”类。它有一个从“PackageItemInfo”类继承的公共字段“icon”。确保您的 appInfo 是“PackageItemInfo”类的任何子类,并且它应该可以工作。如果它不起作用,则给定应用程序的 Android manifest.xml 可能有问题,它无法检索所需的整数。

您可以尝试通过“PackageManager”类的“getApplicationIcon”方法检索图标。再次

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

当然,这意味着您将通过应用程序列表两次,短。

于 2013-12-18T21:02:44.430 回答