I have a RecyclerView that fetchs the user's installed app which include it's icon. I wanna load the app's drawable icon to an ImageView via its URI. Loading the drawable directly using setImageDrawable()
causes hiccups to the UI thread. Lazy loading it using an Image Loader like Glide
was my choice of solution for such problem.
Solution tried: https://stackoverflow.com/a/21993055/13196984 I used that answer but it doesn't load all the icons. Here's my code snippet and a snapshot from my phone.
Code:
How I get the Apps info
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
ArrayList<InstalledApp> installedApps = new ArrayList<>();
for (ApplicationInfo app : apps) {
if (pm.getLaunchIntentForPackage(app.packageName) != null) {
String appName = String.valueOf(app.loadLabel(pm));
int icon = app.icon;
String packageName = app.packageName;
installedApps.add(new InstalledApp(appName, icon, packageName));
}
}
InstalledApp
is my created custom Object but I'll still include it.
public InstalledApp(String appName, int appIcon,String packageName) {
this.appName = appName;
this.appIcon = appIcon;
this.packageName = packageName;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public int getAppIcon() {
return appIcon;
}
public void setAppIcon(int appIcon) {
this.appIcon = appIcon;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
Here's how I load the image from the InstalledApp
Object
if(app.getAppIcon() != 0) {
Uri uri = Uri.parse("android.resource://" + app.getPackageName() + "/" + app.getAppIcon());
Glide.with(activity).load(uri).into(holder.appIconIV);
}
Hope you can help me. :)