16

Is there a way I can get application name, application version and application icon, for the package that is not yet installed? (for some apk file on sdcard)

AZ_
  • 21,688
  • 25
  • 143
  • 191
POMATu
  • 3,422
  • 7
  • 30
  • 42

5 Answers5

74

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.

Tam
  • 1,189
  • 10
  • 15
13

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.

JohnyTex
  • 3,323
  • 5
  • 29
  • 52
Mario
  • 131
  • 3
7

I can get apk icon, version, name by using getPackageArchiveInfo(archiveFilePath, flags) and getApplicationIcon (ApplicationInfo info)

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Nam Vu
  • 5,669
  • 7
  • 58
  • 90
5
// Install a known apk file from the SD-Card
// or launch it if installed. 
// -hsigmond

protected void runAppFromApkFileOnSdCard() {

        final PackageManager pm = getActivity().getPackageManager();
        String apkFileName      = "application_name.apk";
        String fullPath         = "/sdcard/"+apkFileName;
        PackageInfo packageInfo = pm.getPackageArchiveInfo(fullPath, 0);
        Intent intent           = pm.getLaunchIntentForPackage(packageInfo.packageName);

        if( intent == null ){
            File file   = new File(fullPath);
            intent      = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file),
            "application/vnd.android.package-archive");
        }
        startActivity(intent);
    }

Use:

packageInfo.applicationInfo.['name','icon','logo'] etc.

Example Icon:

Drawable icon = packageInfo.applicationInfo.loadIcon(getActivity().getPackageManager());
Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
  • I tried this but always the packageInfo is null. i'm missing any permissions? – 1HaKr Jun 11 '11 at 13:14
  • work for me at Samsung Galaxy Tab 8.9, Android version 3.2. packageInfo is NOT null, intent is null if app not yet installed. – giacatho Jul 05 '12 at 02:39
0

As you might have found out, PackageManager will only be useful to you if the app is installed.

To solve your problem of getting the information without installing the app use the APK extractor, see: http://code.google.com/p/apk-extractor/downloads/detail?name=APK-Extractor-src-1.0.zip&can=2&q=

This project is an attempt to develop a publicly available parser which can parse Android's binary XML. AAPT (Android Asset Packaging Tool) encodes/decodes XML resources in Google's own proprietary binary XML format. It's a common believe that this is a generic WBXML format and any WBXML capable parser can parse it. But this is not true.

If you are planning to explore Android's Binary XML, my code base can help you to start and build service on top of it.

Version 1.0- is capable of parsing Android Manifest, XML layouts etc. and converting DEX/ODEX to CLASS, which can be opened by any de-compiler.

his blog is here: http://prasanta-paul.blogspot.com/2012/03/android-apk-extractor.html

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166