1

我想在我的文件管理器中添加一个选项来显示目录的应用程序图标。下面的代码不起作用;我做错了什么?

ImageView icon;
private static Activity activity;
String temp = mFileMang.getCurrentDir();

} else if (sub_ext.equalsIgnoreCase("apk")) {
                final Drawable appicon;
                try {
                    PackageInfo packageInfo = activity.getPackageManager()
                            .getPackageArchiveInfo(temp,
                                    PackageManager.GET_ACTIVITIES);
                    ApplicationInfo appInfo = packageInfo.applicationInfo;

                    appInfo.sourceDir = temp;
                    appInfo.publicSourceDir = temp;

                    appicon = appInfo
                            .loadIcon(activity.getPackageManager());
                    mViewHolder.icon.setImageDrawable(appicon);

                } catch (Exception e) {
                    mViewHolder.icon.setImageResource(R.drawable.appicon);
                }
4

5 回答 5

2

试试这个..我从sd卡目录中获取图标..从未安装的apk文件中获取图标...

    public class A extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_listing);
        ListView list = (ListView) findViewById(R.id.app_listing);
        ArrayList<PackageInfoStruct> listData = getApks();
        list.setAdapter(new TestAdapter(listData, A.this));

    }

    class PackageInfoStruct {
        String appname = "";
        String pname = "";
        String versionName = "";
        int versionCode = 0;
        Drawable icon;
        String datadir = "";
    }

    public ArrayList<PackageInfoStruct> res;

    private ArrayList<PackageInfoStruct> getApks() {
        try {
            String path = Environment.getExternalStorageDirectory() + "/test";
            File file = new File(path);
            String[] list = file.list();
            res = new ArrayList<PackageInfoStruct>();
            for (String str : list) {
                String not_installed_apk_file = path + "/" + str;
                PackageManager pm = getPackageManager();
                PackageInfo pi = pm.getPackageArchiveInfo(
                        not_installed_apk_file, 0);
                if (pi == null)
                    continue;
                // the secret are these two lines....
                pi.applicationInfo.sourceDir = not_installed_apk_file;
                pi.applicationInfo.publicSourceDir = not_installed_apk_file;
                //
                Drawable APKicon = pi.applicationInfo.loadIcon(pm);
                String AppName = (String) pi.applicationInfo.loadLabel(pm);
                PackageInfoStruct pack = new PackageInfoStruct();
                pack.icon = APKicon;
                pack.pname = AppName;
                res.add(pack);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    private ArrayList<PackageInfoStruct> getInstalledApps() {
        try {
            res = new ArrayList<PackageInfoStruct>();
            List<PackageInfo> packs = getPackageManager().getInstalledPackages(
                    0);

            for (int i = 0; i < packs.size(); i++) {
                PackageInfo p = packs.get(i);

                PackageInfoStruct newInfo = new PackageInfoStruct();
                newInfo.appname = p.applicationInfo.loadLabel(
                        getPackageManager()).toString();
                newInfo.pname = p.packageName;
                newInfo.datadir = p.applicationInfo.dataDir;
                newInfo.versionName = p.versionName;
                newInfo.versionCode = p.versionCode;
                newInfo.icon = p.applicationInfo.loadIcon(this
                        .getPackageManager());
                res.add(newInfo);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

}
于 2013-07-29T13:00:23.497 回答
0

PackageManager..getPackageArchiveInfo(..)该方法仅适用于已安装的 apk,这意味着它只能在已安装的 apk 文件上运行。目前,您可能会在 apk 文件上检查此内容,该文件可能未安装,因此获取包信息失败。

你可以做的是:

  1. 以编程方式解压缩 apk 文件(基本上 apk 文件是一个 zip 文件,您可以通过编程方式将其解压缩。参见http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)

  2. 从 zip 条目中获取 AndroidManifest.xml 文件

  3. 解析 AndroidManifest.xml 文件(请参阅如何解析 .apk 包中的 AndroidManifest.xml 文件

  4. 从中获取 xml 属性 'android:icon'
  5. 再次从 zip 条目中读取图标文件作为位图。
  6. 在任何你想要的地方使用这个图标位图。
于 2013-07-29T09:10:26.723 回答
0

试试这个-

ImageView icon;
private static Activity activity;
String temp = mFileMang.getCurrentDir();

} else if (sub_ext.equalsIgnoreCase("apk")) {
            final Drawable appicon;
            try {
                PackageInfo packageInfo = activity.getPackageManager()
                        .getPackageArchiveInfo(temp,
                                PackageManager.GET_ACTIVITIES);
                ApplicationInfo appInfo = packageInfo.applicationInfo;

                appInfo.sourceDir = temp;
                appInfo.publicSourceDir = temp;

                  PackageManager pm = getPackageManager();
          appicon = pm.getApplicationIcon(appInfo.packageName);

                mViewHolder.icon.setImageDrawable(appicon);

            } catch (Exception e) {
                mViewHolder.icon.setImageResource(R.drawable.appicon);
            }
于 2013-07-29T09:34:04.480 回答
0

要将其添加http://stackoverflow.com/questions/17919151/android-app-icon-filemanager/17924795#17924795到我的代码中,我需要将其删除。最后我得到了这个:

但是当我打开目录时,它只加载 1 个图标并为所有其他应用程序显示它。 https://www.dropbox.com/s/e2bonh3fkfseggf/Screenshot_2013-07-31-13-58-18.png

File file = new File(temp + "/" + mDataSource.get(position));

} else if (sub_ext.equalsIgnoreCase("apk")) {

    try {
        Drawable icon = getApk(file);
        mViewHolder.icon.setImageDrawable(icon);

    } catch (Exception e) {
        mViewHolder.icon.setImageResource(R.drawable.appicon);
    }


private Drawable getApk(File file2) {
    try {
        String path = mFileMang.getCurrentDir();
        File file = new File(path);
        String[] list = file.list();

        for (String str : list) {
            String not_installed_apk_file = path + "/" + str;
            PackageManager pm = mContext.getPackageManager();
            PackageInfo pi = pm.getPackageArchiveInfo(
                    not_installed_apk_file, 0);
            if (pi == null)
                continue;
            // the secret are these two lines....
            pi.applicationInfo.sourceDir = not_installed_apk_file;
            pi.applicationInfo.publicSourceDir = not_installed_apk_file;
            //
            res = pi.applicationInfo.loadIcon(pm);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return res;
}
于 2013-07-31T12:00:42.747 回答
0
PackageManager pm = context.getPackageManager();
PackageInfo info =pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);
于 2016-08-01T22:00:11.723 回答