Retrieving xhdpi app icons from packageManager?

泄露秘密 提交于 2019-11-28 11:36:19

I suspect there is some other reason why the icons look bad (such as resizing within your app), but if you need to do it for any Android version:

// 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);
ApplicationInfo applicationInfo = 
        packagemanager.getApplicationInfo(packageName(), PackageManager.GET_META_DATA);
Resources res = packagemanager.getResourcesForApplication(applicationInfo);
Drawable appIcon = res.getDrawableForDensity(applicationInfo.icon, 
                                             DisplayMetrics.DENSITY_XXXHIGH, 
                                             null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!