Int β Drawable:
Drawable icon = getResources().getDrawable(42, getTheme());
Drawable β Int:
(I assume that you populate the List<AppInfo> apps application whose icons are already in the res/drawable your application)
Once you set R.drawable.app1 to ImageView , you can also specify a tag to identify the resource in 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 come from the server, the only way to save them to disk is then reload them. (or, better, rely on pre-existing image caching solutions like picasso )
UPD: There is no direct way to convert Drawable to int , but in this particular case, you can get int instead of Drawable from PackageManager :
ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1); int icon= applicationInfo.icon;
source share