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