getApplicationIcon() via PackageManager return a white bar image placed in the center that isn't the actual app icon

Viewed 1484

after I invoke getApplicationIcon() like :

context.getPackageManager().getApplicationIcon("com.cmcm.gamemaster");

it return this :

white bar image

a white bar image that placed in the middle. and always happen for specific application, the others in the installed application list never return the wrong icon.

before I invoke that method, I invoke getPackageInfo() about 30 times to build an installed application list. so I commented those codes then the problem gone.

I pay attention in the framework's source(android 6.0.1) but not found any useful information.

have some guy encountered this problem before, or tell me the wrong icon's drawable file name in framework's source code so I can check what's happening.

thank in advance.

3 Answers

Based on your description I assume you want to get a installed application list. Try this to get all applications:

import android.content.pm.ApplicationInfo;
...
List<ApplicationInfo> apps = context.getPackageManager().getInstalledApplications(0);
...

And if you want for a particular one like in your code then try this:

import android.content.pm.ApplicationInfo;
...
ApplicationInfo app = context.getPackageManager().getApplicationInfo("com.cmcm.gamemaster", 0);
Icon icon = app.loadIcon(getPackageManager());
...

You can get icon or title from apps/app since it contains all the info related to an app. Remember to use the exact import statement.

If the above methods doesn't work, refer this post. I'm sure that will be of use.

getPackageManager().getApplicationIcon("APP_PACKAGE_NAME") return Drawable. After getting drawable you can show icon in imageView using imageView.setImageDrawable(drawable).

TIPS:- Getting all installed app icon may hang your UI so you have to do this in a separate thread.

new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final Drawable drawable = mContext.getPackageManager().getApplicationIcon(applicationInfo.packageName);
                    ((Activity) mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            holder.imageView.setImageDrawable(drawable);
                        }
                    });

                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }).start();

hope it helps.

First get the PackageManager instance,

then pass the packageName in the getApplicationIcon(String) which will return the Bitmap!

PackageManager packageManager = mContext.getPackageManager();
Bitmap mBitmap = packageManager.getApplicationIcon(app.package.name);
holder.appIcon.setImageDrawable(mBitmap);
Related