ResolveInfo getIconResource () gives really strange results (wrong icons)
I am creating a list of applications installed on the phone. I am extracting all installed application with PackageManager
and ResolveInfo
. Due to memory issue, I use getIconResource()
instead loadIcon()
.
My question is how to display the correct icons in the imageView using iconResource, int?
EDIT: added code
This is the part where I create a list with the installed apps that are stored in the Arraylist apps
private void loadApps(){
manager = getPackageManager();
apps = new ArrayList<AppDetail>();
if(apps.size()==0) {
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
for (ResolveInfo ri : availableActivities) {
AppDetail app = new AppDetail();
app.label = ri.loadLabel(manager);
app.name = ri.activityInfo.packageName;
app.icon = ri.activityInfo.getIconResource();
app.allowed = false;
apps.add(app);
}
Log.i("applist", apps.toString());
}
}
In ArrayAdapter, I want to put a set of icons in ImageView
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
int id = apps.get(position).icon;
appIcon.setImageResource(id);
This is the result on the phone, its showing weird icons (some apps even use the google plus icon
It also gives errors about lack of resource:
W/ResourceType﹕ getEntry failing because entryIndex 1 is beyond type entryCount 1
W/ResourceType﹕ Failure getting entry for 0x7f030001 (t=2 e=1) in package 0 (error -2147483647)
W/ImageView﹕ Unable to find resource: 2130903041
source to share
The problem was that I was pulling images from the wrong place. This was the code to get the correct icons:
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
String packageName = apps.get(position).name.toString();
try {
Drawable icon = getPackageManager().getApplicationIcon(packageName);
appIcon.setImageDrawable(icon);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
source to share