0

I use IconDialog library to set an icon to my ImageView which user will set them. This library will return a Drawable object. I had no problem working with this library when I had no database as I just called setDrawable function and passed the returned drawable to it, but now I want to store a reference of this drawable to database and I can't find any methods in Drawable.class to get reference of drawable or something and I couldn't find any functions in the library which returns any reference. Any idea?

Mr.Sha1
  • 530
  • 1
  • 3
  • 18

3 Answers3

1

you can use Resource.getIdentifier

int imgId = res.getIdentifier(resourceName, "drawable", context.getPackageName());
indra lesmana
  • 246
  • 2
  • 7
  • That library doesn't add the icon drawable to my project it just send me back an object of Drawable class – Mr.Sha1 Sep 18 '19 at 15:20
  • I know what you want, I think you need to modify the library to change the return as resourceName and include on yout project – indra lesmana Sep 27 '19 at 15:00
0

I think you mean you want to store a Drawable in your SQLite database? You could go with

val icon = BitmapFactory.decodeResource(this.resources, R.drawable.minus_vector_black)
val stream = ByteArrayOutputStream()        
icon.compress(Bitmap.CompressFormat.PNG, 100, stream)
val iconAsByteArray = stream.toByteArray()

and then save that to your database as a BLOB

(code shamelessly stolen from converting Java bitmap to byte array)

Joozd
  • 501
  • 2
  • 14
  • Yea but the drawable is not in my project as R.drawable.XXX. It's just an object of Drawable class. – Mr.Sha1 Sep 18 '19 at 15:19
0

To all whose working with this library and have same problem as me. I found the answer by exploring on the library codes. There is a class called Icon which the IconDialog returns instances of this variable. As Icon's constructor is private you can't instantiate from that class so you should first convert them to Drawable then convert those Drawables to Bitmap (if you don't know how checkout this link).

And for storing Bitmap in DataBase you can simply convert Bitmap to String using this link.
Or store Bitmap in file then store the link or any other ways you know.

Hope this solve your problem :)

Mr.Sha1
  • 530
  • 1
  • 3
  • 18