我正在做一个需要每个应用程序的更高分辨率图标的应用程序。
我从今天早上开始做了一些研究,但没有找到任何答案。
以下是其中两个以及我的错误。
仅供参考,我已经从packagemanager中检索了所有“packageinfo”,我找到了我需要的一切,但高分辨率图标...
1。使用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;
}
}
我的问题是我不明白要为componentName添加什么。如何使用packagemanager提供的信息“构建”它?
2.其他解决方案 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);
对于这一行,我对这一行有疑问:
Drawable appIcon = res.getDrawable(appInfo.icon);
我得到“icon”错误:
图标无法解析或不是字段
答案 0 :(得分:1)
这只是猜测,因为我不是一个特别优秀的Android程序员,但是: 在第一个解决方案中,您可以通过PackageManager类的getInstalledApplications(int标志)方法检索设备上安装的每个应用程序的列表。 看到 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'类。它有一个公共字段'icon',它继承自'PackageItemInfo'类。确保你的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));
}
当然,这意味着你要在应用程序列表中使用两次。
答案 1 :(得分: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);
}
}