gpt4 book ai didi

android - 如何使用包管理器查找高分辨率图标

转载 作者:行者123 更新时间:2023-11-29 00:24:36 25 4
gpt4 key购买 nike

我正在做一个应用,每个应用都需要更高分辨率的图标。

从今天早上开始,我做了一些研究,但没有找到有效的答案。

这是其中两个问题,对我来说有什么问题。

仅供引用,我已经从 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);

关于“图标”的错误:

图标无法解析或不是字段

最佳答案

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

//获取高分辨率图标

             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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com