Search code examples
androidfileiconsdrawable

Android App Icon Filemanager


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

Solution

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