1

How to save Icon from PackageName to shared preference in String format and then I can restore the string from preference and set it into my image

SharedPreferences.Editor editor = getActivity().getSharedPreferences("MyPrefGameAppProgram", MODE_PRIVATE).edit();
                editor.putString("GameProgram"+CounterGameProgram, p.applicationInfo.loadLabel(getActivity().getPackageManager()).toString()+ "%" +p.packageName+ "%" +p.versionName+ "%" +p.versionCode+ "%" + p.applicationInfo.loadIcon(getActivity().getPackageManager()););
                editor.apply();

set it again

for(int i=1; i<=Integer.parseInt(TotalGameProgram); i++){
        String currentStringX = prefsx.getString("GameProgram"+i, "No Data");
        String[] separatedX = currentStringX.split("%");
        String appnameX = separatedX[0];
        String apppackagenameX = separatedX[1];
        String appversionnameX = separatedX[2];
        int appversioncodeX = Integer.parseInt(separatedX[3]);
        /*String appiconX = separatedX[4];*/


        /*modelMyAppsInstalledApps.add(new ModelMyAppsInstalledApps(appnameX, apppackagenameX, appversionnameX, appversioncodeX, appiconX));*/
    }

Iam struggling because the icon format that I got from that library is Drawable so I can't save it to preference, Any solution?

1 Answers1

1

I'am struggling because the icon format that I got from that library is Drawable so I can't save it to preference

Yes, instead of applicationInfo.loadIcon use applicationInfo.icon which return int drawable id and save this id in preference.

To get drawable from this id use PackageManager like:

Resources res = getPackageManager.getResourcesForApplication(p.applicationInfo);
Drawable icon = res.getDrawable(<drawable_id>);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213