我正在尝试从未安装的应用中获取一些信息(名称和图标)。基本上o得到了apk文件,我还没有安装它,但我想在列表中显示它的名称和图标。
我正在尝试这样做:
public void getReceivedApps() {
packageManager = getPackageManager();
System.out.println("TRANSFER-RECORDActivityThe received filePath is : " + receivedFilePath);
PackageInfo packageInfo = packageManager.getPackageArchiveInfo(receivedFilePath, PackageManager.GET_GIDS);
System.out.println("TRANSFER RECORD ACTIVYT : O TO STRING TO PACKAGE INFO : " + packageInfo.toString());
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
Log.d(this.getClass().getName(), "packaginf0 : " + applicationInfo.toString());
String appName = applicationInfo.loadLabel(packageManager).toString();
System.out.println("TRANSFER RECORD ACTIVITY : THE APPNAME IS : " + appName);
Drawable icon = packageManager.getPackageArchiveInfo(receivedFilePath, PackageManager.GET_GIDS).applicationInfo.loadIcon(packageManager);
//then add it to a list.
}
问题是它在loadIcon和loadLabel指令中给出了nullpointerexception。
顺便说一下,receivedFilePath
字符串包含该apk文件的文件路径(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+getIntent().getStringExtra("receivedFilePath"); )
这个以字符串形式发送的receivedFilePath具有文件名(不是应用程序)
答案 0 :(得分:2)
发现了类似的问题,希望有所帮助:
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);