我正在开发一个应用程序,需要每个应用程序都有更高分辨率的图标。
从今天早上起我做了一些研究,但没有找到有效的答案。
这是其中两个以及对我来说有什么问题。
仅供引用,我已经从包管理器中检索了所有“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);
关于“图标”的错误:
图标无法解析或不是字段
最佳答案
试试这个,它与您的代码类似,但似乎对我有用:
//获取高分辨率图标
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);
}
}
关于android - 如何使用包管理器查找高分辨率图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664290/