【问题标题】:Get apk file icon, version, name获取apk文件图标、版本、名称
【发布时间】:2011-04-14 09:51:55
【问题描述】:

对于尚未安装的包,我有没有办法获取应用程序名称、应用程序版本和应用程序图标? (对于 sdcard 上的一些 apk 文件)

【问题讨论】:

  • 你能找到版本号吗,如果是,你能把你的答案发布出来吗。

标签: android


【解决方案1】:

我只是花了比我承认的更多的时间来寻找解决方案......并找到了它! :) :)

浏览此参考后: http://code.google.com/p/android/issues/detail?id=9151 我发现了从尚未安装的 .apk 文件中获取图标和名称(即标签)的技巧。

我希望现在帮助其他人还为时不晚:

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

经过轻度测试,它似乎可以工作......哇。

【讨论】:

【解决方案2】:

我试过了:

PackageInfo info = getPackageManager().getPackageArchiveInfo(fullPath, 0);
ApplicationInfo appinfo = info.applicationInfo;
label = getPackageManager().getApplicationLabel(appinfo).toString();
img = getPackageManager().getApplicationIcon(info.packageName);

但如果没有安装 apk,此代码将不起作用。

【讨论】:

    【解决方案3】:

    我可以使用getPackageArchiveInfo(archiveFilePath, flags)getApplicationIcon (ApplicationInfo info)获取apk图标、版本、名称

    【讨论】:

    • 不适用于未安装的应用:(,只返回默认应用图标。
    • 这不应该是一个可接受的答案,因为这不能回答问题。 @POMATu 请将 Tam 的答案标记为已接受的答案
    【解决方案4】:
    // 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);
        }
    

    用途:

    packageInfo.applicationInfo.['name','icon','logo'] etc.
    

    示例图标:

    Drawable icon = packageInfo.applicationInfo.loadIcon(getActivity().getPackageManager());
    

    【讨论】:

    • 我试过了,但 packageInfo 总是为空。我缺少任何权限?
    • 在三星 Galaxy Tab 8.9,Android 版本 3.2 上为我工作。 packageInfo 不为空,如果尚未安装应用,intent 为空。
    【解决方案5】:

    您可能已经发现,PackageManager 只有在安装了应用程序后才会对您有用。

    要解决您在不安装应用的情况下获取信息的问题,请使用 APK 提取器,请参阅:http://code.google.com/p/apk-extractor/downloads/detail?name=APK-Extractor-src-1.0.zip&can=2&q=

    这个项目是尝试开发一个公开可用的解析器 它可以解析Android的二进制XML。 AAPT(Android 资产打包 工具)在 Google 自己的专有二进制文件中编码/解码 XML 资源 XML 格式。人们普遍认为这是一种通用的 WBXML 格式 并且任何支持 WBXML 的解析器都可以解析它。但事实并非如此。

    如果您打算探索 Android 的二进制 XML,我的代码库可以 帮助您在此基础上启动和构建服务。

    版本 1.0- 能够解析 Android Manifest、XML 布局等。 并将DEX/ODEX转换为CLASS,任何人都可以打开 反编译器。

    他的博客在这里: http://prasanta-paul.blogspot.com/2012/03/android-apk-extractor.html

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 2015-07-06
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多