1
        String apkPath = apk.getAbsolutePath();
        Log.i(getClass().getName(), "addApk(): apkPath = " + apkPath);
        PackageInfo localPackageInfo = pm.getPackageArchiveInfo(apkPath, 0);
        localPackageInfo.applicationInfo.sourceDir = apkPath;//! null point exception

无法获取 apk 的图标、名称、版本等。我也尝试了幻数 8192 而不是 0,但也不起作用。有人有什么建议吗?提前谢谢!


我将代码更改为:

        PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        if (pi != null) {
            ApplicationInfo ai = pi.applicationInfo;
            Drawable appIcon = pm.getApplicationIcon(ai);
            String appName = pm.getApplicationLabel(ai).toString();
            String appVersion = pi.versionName;
            String pkgName = ai.packageName;

            Log.i(getClass().getName(), "pkgName = " + pkgName + ", appName = " + appName + ", appVersion = " + appVersion);
            adapter.add(new InstallItem(pkgName, appIcon, appName, appVersion));
        }

并得到这样的输出:

pkgName = com.chaozh.iReaderFree, appName = com.chaozh.iReaderFree, appVersion = 1.3.2.0 ...

如您在此处看到的,我无法获取应用程序名称,并且其图标始终是默认的相同图标。我厌倦了市场上的其他安装程序,他们工作得很好,所以有人告诉我该怎么做吗?谢谢。

4

3 回答 3

4

您可以使用 PackageManager 方法getPackageArchiveInfo从 APK 文件中检索信息。假设您的 APK 位于 SD 卡的根目录中,您可以使用以下代码:

    String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() 
                   + "/MyApp.apk";
    PackageManager pm = getPackageManager();

    PackageInfo info = pm.getPackageArchiveInfo(apkPath, 
                           PackageManager.GET_ACTIVITIES);
    //Log.i("ActivityInfo", "Package name is " + info.packageName);

    for (android.content.pm.ActivityInfo a : info.activities) {
        Log.i("ActivityInfo", a.name);
    }

您可以在 ActivityInfo 和 PackageInfo 对象中找到大量附加信息。

于 2012-06-07T09:14:58.753 回答
1

这是正确的答案,有一些技巧(SDK> 8!):

public Drawable getApkIcon(String path){      
    PackageManager pm = getPackageManager();      
    PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);      
    if(info != null){      
        ApplicationInfo appInfo = info.applicationInfo;

        if(Build.VERSION.SDK_INT >= 8){
            appInfo.sourceDir = path;
            appInfo.publicSourceDir = path;
        }

        return appInfo.loadIcon(pm);
    }           
    return null;
}
于 2012-06-15T12:54:19.317 回答
0

最简单的方法 只需使用其获取任何应用程序的图标packageName

public static final Drawable getIconOfAppUsingPackageName(Context context,String packageName)
{
 Drawable icon = context.getPackageManager().getApplicationIcon(packageName);
 imageView.setImageDrawable(icon);
 return icon;
}
于 2015-04-08T08:51:36.603 回答