Android 如何在我的文件管理器中显示apk图标

Android 如何在我的文件管理器中显示apk图标,android,icons,apk,Android,Icons,Apk,我想在我的列表视图中显示apk文件的图标 如何获取apk的图标 if (file.getPath().endsWith(".apk")) { String filePath = file.getPath(); PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);

我想在我的列表视图中显示apk文件的图标

如何获取apk的图标

    if (file.getPath().endsWith(".apk")) {
        String filePath = file.getPath();
        PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
        if(packageInfo != null) {
            ApplicationInfo appInfo = packageInfo.applicationInfo;
            if (Build.VERSION.SDK_INT >= 8) {
                appInfo.sourceDir = filePath;
                appInfo.publicSourceDir = filePath;
            }
            Drawable icon = appInfo.loadIcon(context.getPackageManager());
            bmpIcon = ((BitmapDrawable) icon).getBitmap();
        }
    }
最重要的一行是appInfo.publicSourceDir=filePath

这是一个错误。看


空检查
packageInfo!=如果.apk未正确解析,则存在null。

这应该被认为是正确答案,因为Varundroid提出的解决方案不适用于未安装的apk文件。@Jens完全同意您的看法。最好的答案应该永远被接受。我编辑了我的答案。谢谢你指出这个问题。干杯