user2517419
user2517419

Reputation:

How can I get the application's icon from the package name?

I have tried various solution from stack overflow with no luck. What I want.

  1. I know package name of different applications.
  2. I want to get application Icon from those package name.
  3. Show those icons in Image View.

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

Answers (5)

Mihir Shah
Mihir Shah

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

Pir Fahim Shah
Pir Fahim Shah

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

T_V
T_V

Reputation: 17580

Try this:

try
{
    Drawable icon = getContext().getPackageManager().getApplicationIcon("com.example.testnotification");
    imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}

Upvotes: 164

saigopi.me
saigopi.me

Reputation: 14908

try
{
    Drawable drawable = getPackageManager()
            .getApplicationIcon("com.whatsapp");
    imageView.setImageDrawable(drawable);
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}

Upvotes: 1

thecoolmacdude
thecoolmacdude

Reputation: 2277

This also works:

try
{
    Drawable d = getPackageManager().getApplicationIcon(getApplicationInfo());
    my_imageView.setBackgroundDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
    return;
}

Upvotes: 10

Related Questions