2

I want to add an option in my file manager to show the App Icons of a directory. The code below didn't work; what did I do wrong?

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);
                }
Prune
  • 76,765
  • 14
  • 60
  • 81
df1e
  • 50
  • 1
  • 9

5 Answers5

3

try this.. i fetch the icon from the sd card directory ..icon from the apk files which are not installed ...

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

}
user1140237
  • 5,015
  • 1
  • 28
  • 56
0

PackageManager..getPackageArchiveInfo(..) method will work only for installed apks, means it will operate only on an apk file, which has already been installed. Currently you maybe checking this on an apk file, which might not be installed and thus getting package info is failed.

What you can do is:

  1. Unzip the apk file programatically (Basically apk file is a zip file, and you can extract it programatically. see http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically) )

  2. Get the AndroidManifest.xml file from the zip entry

  3. Parse the AndroidManifest.xml file ( see How to parse the AndroidManifest.xml file inside an .apk package )

  4. Get the xml attribute 'android:icon' from it
  5. Read the icon file from the zip entry again as bitmap.
  6. use this icon bitmap wherever you want.
Community
  • 1
  • 1
Suji
  • 6,044
  • 2
  • 19
  • 17
0

Just Try with this-

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);
            }
T_V
  • 17,440
  • 6
  • 36
  • 48
0

to add this http://stackoverflow.com/questions/17919151/android-app-icon-filemanager/17924795#17924795 to my code i need to cut it. finally i got this:

but when I open the directory it load only 1 icon and show it for all other apps too. 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;
}
df1e
  • 50
  • 1
  • 9
0
PackageManager pm = context.getPackageManager();
PackageInfo info =pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);
Xiangshi Yin
  • 31
  • 1
  • 3