1

I just want to store a Drawable image file in Realm database, but after assigning the value in Realm Object, it's showing one error message that "error: Type android.graphics.drawable.Drawable of field is not supported".

public class AppsList extends RealmObject {
    public Drawable getAppIcon() {
      return appIcon;
    }
    public void setAppIcon(Drawable appIcon) {
      this.appIcon = appIcon;
    }
}

    Intent i = new Intent(Intent.ACTION_MAIN, null);
    i.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> availableActivities = manager.queryIntentActivities(
            i, 0);
    for (ResolveInfo ri : availableActivities) {
        realm.beginTransaction();

        appsIconList = realm.createObject(AppsList.class);

        appsIconList.setAppLabel(ri.loadLabel(manager).toString());
        appsIconList.setAppPackageName(ri.activityInfo.packageName.toString());
        appsIconList.setAppIcon(ri.activityInfo.loadIcon(manager).toString());

        realm.commitTransaction();
    }

Please kindly go through my post and let me that how to store drawable file in Realm Database.

Anshuman Pattnaik
  • 883
  • 3
  • 16
  • 37
  • 1
    you can store the drawable id as integer. and retrieve it later. if its not your application drawable, then you should store the activityInfo componentName. or worst case, convert it to Bitmap then store it as base64 and later base64 > bitmap – Kosh Dec 10 '15 at 06:44
  • what is `"Drawable image file"` and how is it related to `Drawable` class ? – pskink Dec 10 '15 at 07:23
  • So i want to populate Package manager app icons, and this function returns drawable. I have updated my post please kindly go through it. – Anshuman Pattnaik Dec 10 '15 at 07:26
  • so what is wrong in calling `getApplicationIcon(ApplicationInfo info)` or `getActivityIcon(ComponentName activityName)` or any other `PackageManager` method? – pskink Dec 10 '15 at 07:31
  • can you please tell me the complete code ??? – Anshuman Pattnaik Dec 10 '15 at 07:34
  • the code for `getApplicationIcon` / `getActivityIcon` ? – pskink Dec 10 '15 at 07:42
  • yes can you please tell me ?? – Anshuman Pattnaik Dec 10 '15 at 07:42
  • what problems do you have with `getApplicationIcon` / `getActivityIcon` ? – pskink Dec 10 '15 at 07:44
  • Why aren't you just storing the icon resource given to you with `getIconResource()`? Although I guess you can load the drawable and convert it to `byte[]` http://stackoverflow.com/a/4435827/2413303 – EpicPandaForce Dec 10 '15 at 07:57

2 Answers2

1

I don't think you can do that. Realm only support these types :

INTEGER,
BOOLEAN,
STRING,
BINARY,
DATE,
FLOAT,
DOUBLE,

Reference from : RealmFieldType.java

Actually you can also store the id of the drawable in Integer format but still it's also not recommended because id will be generated everytime you build the project and there is a chance it will changed from what you saved last time.

In case you have the name of the drawable you can also store the drawable name to Realm database and get the id of the drawable using this code

Resources resources = context.getResources();
int resourceId = resources.getIdentifier(name, "drawable", 
   context.getPackageName());
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
0

Realm doesn't support Drawable directly. You would have to convert the Drawable into a serializable byte[] first. That probably means converting it to an Bitmap first. The answer provided by @EpicPandaForce is a good start: Drawable to byte[]

It is a lot of overhead though. I would suggest saving the Drawable as a file on the filesystem and save the path in Realm instead.

Community
  • 1
  • 1
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53