2

I am trying to implement a login (no register) service in my application using Realm as database. It is the first time I am trying to use this so I am really new to Realm. I have read Realm documentation but I wasn't able to figure out how can I add Image to Realm and use it in my app.

The scenario is like this: I have predefined username and password set and when user logins with that username and password, I want to update the Account Header Image in Navigation Drawer to that username and password. Also, user can update the Account Header Image too.

I hope you guys were able to understand my scenario. Please help me with this.

Any help is greatly appreciated. Thanks. :)

Anmol

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
Anmol
  • 77
  • 1
  • 10
  • 1
    I would never suggest to save image in database, you will have overhead of converting it to some other type, typically base64 string, atob and btoa conversions. Instead, just save path to that image. It can be local path or url. – Chintan Soni Sep 02 '16 at 11:14
  • Then, how can I solve my above scenario? Do you know any better alternative for that? I'd be really grateful to you if you can tell me. – Anmol Sep 02 '16 at 12:12

4 Answers4

3

Realm documentations say the following limitation:

Strings and byte arrays (byte[]) cannot be larger than 16 MB.

The recommended way is to save the path to a file instead of the actual image.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Ok. But do you know how the images are displayed from server in certain apps? How do they achieve it? They really save path to a file for every image to do so? – Anmol Sep 02 '16 at 12:14
  • Technically I stored a relative URL for obtaining the image with `SERVER_URL + "/files" + stuff.getImageUrl()` and gave it to Glide which handled it for me – EpicPandaForce Sep 02 '16 at 12:19
  • Here is an example https://github.com/Zhuinden/rx-realm-recyclerview-experiment/blob/master/app/src/main/java/com/zhuinden/rxrealm/path/cat/CatViewHolder.java#L49 – EpicPandaForce Sep 02 '16 at 12:27
  • Oh. To understand it little better is there any code or previous question asked on Stack to which I can refer? – Anmol Sep 02 '16 at 12:28
  • Thank you @EpicPandaForce! :D – Anmol Sep 02 '16 at 12:31
  • hey, do you know how can I update the image when user updates the image on his own? – Anmol Sep 02 '16 at 12:57
  • There are different solutions, if the URL is the same then resource can be versioned somehow, otherwise for example what I did where users had their own profiles was that when the image was uploaded, the generated filename contained the date/time of creating the file, thus any new profile image was available via a new URL; you can also invalidate cache from time to time on start-up just to make sure and stuff. – EpicPandaForce Sep 02 '16 at 13:09
  • Any example available to achieve the exact thing? – Anmol Sep 03 '16 at 05:20
0

One of the ways to do so is to convert image into base64 string and store it as a string in realm database.

While retrieving, decode that base 64 string to image and use it.

Here is a link of how to convert image to base64

Another not so reliable solution is download the image into some file and store the file path in realm

Community
  • 1
  • 1
Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
-1

If blob data type is available in realm then you can store image byte array in blob

Suraj
  • 72
  • 4
-2

If you have larger bitmaps (images), it is better to store the images on disk, and store the path in Realm.

Yurii Bulan
  • 143
  • 1
  • 1
  • 7