14

I want to convert Drawable into int and then vice versa.Basically I want to save Arraylist object into sharedPrefrence. For that purpose I have Implement Gson to Srtring convertion Method. If I use here Drawable instead of int then Gson String convertion take alot of time. so I want to use int instead of Drawable.

 private List<AppInfo> apps = null;

   public void setIcon(int icon) {
    this.icon = icon;
}

   apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon()));

Where AppInfo here is

    public AppInfo(String appname, String pname, String versionName, int versionCode, int icon, int color) {
    this.appname = appname;
    this.pname = pname;
    this.versionName = versionName;
    this.versionCode = versionCode;
    this.icon = icon;
    this.color = color;
}

Here is source of Converting ArrayList of Custom object into String so that i can save it to SharedPrefrence.

            Gson gson = new Gson();
            apps.get(number).setColor(picker.getColor());
            String JsonAppsdata = gson.toJson(apps);
            System.out.println("Storing="+JsonAppsdata);
            utility.StoreData(getApplicationContext(), JsonAppsdata);
M.ArslanKhan
  • 3,640
  • 8
  • 34
  • 56
  • Do you have all icons in your resources? Or are you downloading them from the server? – Konstantin Loginov Dec 27 '15 at 16:59
  • And how do you populate List apps? – Konstantin Loginov Dec 27 '15 at 17:08
  • At first time I populate it from the res/drawable then after this if some one (user) want to change that icon with some one in system app icon then i need to get system install application icon. In short, some time I need to get from res/drawable and some times i need to get icons from install application's icon from the system, by this mContext.getPackageManager().getApplicationIcon(apps.get(position).getPname()) – M.ArslanKhan Dec 28 '15 at 12:15
  • So what stops you from keeping this mContext.getPackageManager().getApplicationIcon() int into your AppInfo? – Konstantin Loginov Dec 28 '15 at 12:18
  • I can't use Drawable in AppInfo, I used here int instead of Drawable because of a case that i have discussed above. Now I need to convert this into int. – M.ArslanKhan Dec 28 '15 at 12:23
  • mContext.getPackageManager().getApplicationIcon() this returns Drawable I need to convert it into int so that my AppInfo not affect. – M.ArslanKhan Dec 28 '15 at 12:25
  • You can call `getApplicationInfo()` method(http://developer.android.com/reference/android/content/pm/PackageManager.html) instead. It returns `ApplicationInfo`, which contains icon's int. And then convert it to drawable (as menioned in my answer :-) ) – Konstantin Loginov Dec 28 '15 at 12:37
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99124/discussion-between-tarikhelian-and-konstantin-loginov). – M.ArslanKhan Dec 28 '15 at 12:40

3 Answers3

7

Int -> Drawable:

Drawable icon = getResources().getDrawable(42, getTheme());

Drawable -> Int:

(I assume, that you're populating List<AppInfo> apps with app's whose icons are already in res/drawable folder of your app)

Once you set your R.drawable.app1 to ImageView, you can also give it a tag to identify the resource in the ImageView later:

    ImageView appIcon1ImageView = (ImageView)findViewById(R.id.app_icon_1);
    appIcon1ImageView.setImageDrawable(getDrawable(R.drawable.app1));
    appIcon1ImageView.setTag(R.drawable.app1);

    ......
    // Once you need to identify which resource is in ImageView
    int drawableId = Integer.parseInt(appIcon1ImageView.getTag().toString());

If your icons are coming from server - the only way is to store them to disk and then re-load them. (or, better, rely on the already existing image-caching solutions like picasso)

UPD: There's no direct way of converting Drawable into int, but in this particular case, it's possible to get the int, instead of Drawable from PackageManager:

ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1);
int icon= applicationInfo.icon;
Community
  • 1
  • 1
Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95
  • There is problem that I am facing is resource id not found exception while ApplicationInfo applicationInfo=mContext.getPackageManager(). getApplicationInfo(apps.get(position).getPname(),1); int icon= applicationInfo.icon; apps.get(position).setIcon(applicationInfo.icon); //while getting back int->Drawable `Drawable icon = getResources().getDrawable(apps.get(i).getIcon()); ImageView imageView = (ImageView) rootView.findViewById(imageArray[i]); imageView.setImageDrawable(icon);` – M.ArslanKhan Dec 28 '15 at 17:22
  • How can I do the same for a drawableLeft? I'm using drawableLeft with a TextView and retrieving it's values using getCompoundDrawables method. So how can I convert that to an int. – Tirth Patel Jul 07 '18 at 05:06
1

This is how we can fetch app icon and set it for an imageview.

                   applicationInfo=mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),PackageManager.GET_META_DATA);
                   int icon= applicationInfo.icon;
                   Rsources resources=mContext.getPackageManager().getResourcesForApplication(applicationInfo);

                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        holder.itemIcon.setImageDrawable(resources.getDrawable(icon,null));
                    }else
                    {
                         holder.itemIcon.setImageDrawable(resources.getDrawable(icon));

                    }
Lins Louis
  • 2,393
  • 2
  • 25
  • 30
0

The only solution that worked for me was in the chat by @KonstantinLoginov, but instead of using:

val drawable = context.getPackageManager().getApplicationIcon(applicationInfo),

I passed in the packageName:

val drawable = context.getPackageManager().getApplicationIcon(packageName) which is String and can easily be passed around.

Community
  • 1
  • 1
waseefakhtar
  • 1,373
  • 2
  • 24
  • 47