11

I want to show icon of apk file in my listview.

How can i get icon of apk?

Nam Vu
  • 5,669
  • 7
  • 58
  • 90

1 Answers1

32
    if (file.getPath().endsWith(".apk")) {
        String filePath = file.getPath();
        PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
        if(packageInfo != null) {
            ApplicationInfo appInfo = packageInfo.applicationInfo;
            if (Build.VERSION.SDK_INT >= 8) {
                appInfo.sourceDir = filePath;
                appInfo.publicSourceDir = filePath;
            }
            Drawable icon = appInfo.loadIcon(context.getPackageManager());
            bmpIcon = ((BitmapDrawable) icon).getBitmap();
        }
    }

The most important line is appInfo.publicSourceDir = filePath;

This is a bug. See http://code.google.com/p/android/issues/detail?id=9151

The null check packageInfo != null is there in case the .apk is not parsed correctly.

phlip9
  • 48
  • 1
  • 6
NkD
  • 336
  • 4
  • 3
  • This should be accepted as the correct answer as the solution proposed by Varundroid does NOT work with non-installed apk-files. – jenzz Aug 20 '12 at 09:49
  • @Jens totally agree with you. Best answer should always be accepted. I edited my answer. Thanks for pointing the issue. Cheers. – Varundroid Oct 16 '12 at 11:12