Reputation:
I have tried various solution from stack overflow with no luck. What I want.
For example, I have a package name com.example.testnotification
. How to get this apps icon and show it in an ImageView?
Upvotes: 73
Views: 63932
Reputation: 51
I tried all of the above answers, but was only getting icons from system apps, issue faced in Android 11 (API level 30) or higher, a little digging led me to this docs page which said that you need to add the following permission in your AndroidManifest file:
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
And now all icons are visible.
I used the following piece of code to implement the said functionality using DataBinding:
@BindingAdapter("getImageFromPackageName")
fun ImageView.getImageFromPackageName(packageName: String) {
try {
val icon = context.packageManager.getApplicationIcon(packageName)
this.setImageDrawable(icon)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
}
Hope that helps, Thanks!
Upvotes: 2
Reputation: 10623
If you want to get the icon (picture) of any installed application from its package name, then just copy and paste this code. It will work:
try
{
Drawable d = getPackageManager().getApplicationIcon("com.AdhamiPiranJhandukhel.com");
my_imageView.setBackgroundDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
return;
}
Upvotes: 14
Reputation: 17580
Try this:
try
{
Drawable icon = getContext().getPackageManager().getApplicationIcon("com.example.testnotification");
imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
Upvotes: 164
Reputation: 14908
try
{
Drawable drawable = getPackageManager()
.getApplicationIcon("com.whatsapp");
imageView.setImageDrawable(drawable);
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
Upvotes: 1
Reputation: 2277
This also works:
try
{
Drawable d = getPackageManager().getApplicationIcon(getApplicationInfo());
my_imageView.setBackgroundDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
return;
}
Upvotes: 10