ResolveInfo getIconResource() gives very strange results (incorrect icon)
I’m creating a list of apps installed on my phone. I’m using PackageManager
and ResolveInfo
to retrieve all installed applications. Due to memory issues, I use getIconResource()
instead of loadIcon().
My question is how to use iconResource int to display the correct icon in the imageView?
Edit: Add code
This is the part where I create a list where installed applications are stored in 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 the ArrayAdapter, I want to put the icon in the 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, which shows strange icons (some apps even use the Google plus icon :
It also gives an error about resource not found:
W/ResourceType:getEntry failed because entryIndex 1 exceeded type entryCount 1
W/ResourceType: Failed to get entry for 0x7f030001 (t=2 e=1) in package 0 (error -2147483647)
W/ImageView: Resource not found: 2130903041
Solution
The problem is that I retrieve the image from the wrong place. Here is the code to get the correct icon:
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();
}