Get apk file icon, version, name

I just spent more hours than I'll ever admit looking for the solution to this.. and found it! :) :)

After wandering into this reference: http://code.google.com/p/android/issues/detail?id=9151 I found out the trick to obtaining the icon and name(label, that is) from .apk files that have NOT BEEN INSTALLED.

I hope it's not too late to help others with this:

 String APKFilePath = "mnt/sdcard/myapkfile.apk"; //For example...
 PackageManager pm = getPackageManager();
 PackageInfo    pi = pm.getPackageArchiveInfo(APKFilePath, 0);

 // the secret are these two lines....
 pi.applicationInfo.sourceDir       = APKFilePath;
 pi.applicationInfo.publicSourceDir = APKFilePath;
 //

 Drawable APKicon = pi.applicationInfo.loadIcon(pm);
 String   AppName = (String)pi.applicationInfo.loadLabel(pm);

After light testing this, it seems to work... whew.


I have tried:

PackageInfo info = getPackageManager().getPackageArchiveInfo(fullPath, 0);
ApplicationInfo appinfo = info.applicationInfo;
label = getPackageManager().getApplicationLabel(appinfo).toString();
img = getPackageManager().getApplicationIcon(info.packageName);

But if the apk isn't installed this code won't work.

Tags:

Android