开发者

Get apk file icon, version, name

Is there a way I can get applic开发者_运维百科ation name, application version and application icon, for the package that is not yet installed? (for some apk file on sdcard)


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.


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


// 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());


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜