I'm having some trouble retrieving icons for installed Android App. The following code returns icons for all apps, however, some apps are returned with small icons (perhaps from drawable - ldpi folder) while s开发者_JAVA百科ome with large icons.
List<PackageInfo> package = getPackageManager().getInstalledPackages(0);
for(PackageInfo p : package){
iApp.icon = p.applicationInfo.loadIcon(getPackageManager());
}
How Can I get icons which are all of the same size, just like in Manage Application section of Android?
I want to add something to previous answer what applies to Android 3.0 and greater.
You can notice that icons retrieved in this standard way (i mean applicationInfo.loadIcon and other typical methods) are smaller than icons you see in launcher app. Scale up just makes icons blurrier. If you want big icons you can use next code (i took it from launcher source code you can find here and changed a bit). Pay your attention to activityManager.getLauncherLargeIconDensity
method.
public Drawable getFullResDefaultActivityIcon() {
return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
}
public Drawable getFullResIcon(Resources resources, int iconId) {
Drawable d;
try {
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
int iconDpi = activityManager.getLauncherLargeIconDensity();
d = resources.getDrawableForDensity(iconId, iconDpi);
} catch (Resources.NotFoundException e) {
d = null;
}
return (d != null) ? d : getFullResDefaultActivityIcon();
}
public Drawable getFullResIcon(String packageName, int iconId) {
Resources resources;
try {
resources = mContext.getPackageManager().getResourcesForApplication(packageName);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
}
public Drawable getFullResIcon(ResolveInfo info) {
return getFullResIcon(info.activityInfo);
}
public Drawable getFullResIcon(ActivityInfo info) {
Resources resources;
try {
resources = mContext.getPackageManager().getResourcesForApplication(info.applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
int iconId = info.getIconResource();
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
}
private Drawable getAppIcon(ResolveInfo info) {
return getFullResIcon(info.activityInfo);
}
Hope it helps someone.
Apps might just have different sized icons. The Android Settings app loads icons just like you. It scales the icons when displaying them.
Here's a snippet from packages/apps/Settings/res/layout/manage_applications_item.xml:
<ImageView android:id="@+id/app_icon"
android:layout_width="@android:dimen/app_icon_size"
android:layout_height="@android:dimen/app_icon_size"
android:layout_marginRight="11dip"
android:layout_gravity="center_vertical"
android:scaleType="fitCenter"/>
Although, this has been answered a long time back, here is the complete function. Hope it will help others.
To get the icon, you should query for icon for different dpi.
Try this:
public static Drawable getIconFromPackageName(String packageName, Context context)
{
PackageManager pm = context.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
{
try
{
PackageInfo pi = pm.getPackageInfo(packageName, 0);
Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
int displayMetrics[] = {DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_TV};
for (int displayMetric : displayMetrics)
{
try
{
Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon, displayMetric);
if (d != null)
{
return d;
}
}
catch (Resources.NotFoundException e)
{
// Log.d(TAG, "NameNotFound for" + packageName + " @ density: " + displayMetric);
continue;
}
}
}
catch (Exception e)
{
// Handle Error here
}
}
ApplicationInfo appInfo = null;
try
{
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
}
catch (PackageManager.NameNotFoundException e)
{
return null;
}
return appInfo.loadIcon(pm);
}
You must define in App class ;
private Drawable appIcon;
public Drawable getappIcon(){
return this.appIcon;
}
if you use RecyclerViewAdapter ;
List<App> list_app;
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.appIcon.setImageDrawable(list_app.get(position).getappIcon());
}
in MainActivity ;
private List<App> appList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
appList = new ArrayList<App>();
for (ApplicationInfo packageInfo : packages) {
Drawable drawableIcon =packageInfo.loadIcon(getPackageManager());
appList.add(new App (drawableIcon));
}
精彩评论