2

I want to know the best way to store images from and api into room db. I'm making a sport application where I receive data and images from an api. When I'm in online mode, the images are loaded using the urls provided by the api but when offline, images should be stored and retrieved from the database in offline mode. I want to know how to convert that url of image to an image and load it in offline mode.

PS : more than 100 images will be saved, does room db fit with that? Thank you.

theduck
  • 2,589
  • 13
  • 17
  • 23
Taki
  • 3,290
  • 1
  • 16
  • 41
  • Could you please more details about what you have done so far? I found this article which gives more details about using offline data: https://proandroiddev.com/add-offline-support-using-room-ecee194c503e – Rajan Panneer Selvam Nov 10 '19 at 22:55
  • i mean i never tried it before and i also looked up some articles but i didn't get it , i have no idea and didn't find any helpful topic to do that thats why i asked for help as it is my first time to do with room db – Taki Nov 11 '19 at 00:09
  • 1
    store your image in cache using picasso. – L2_Paver Nov 11 '19 at 01:26
  • @L2_Paver Make it an answer. – Mahdi-Malv Nov 11 '19 at 07:38

2 Answers2

2

store your image in cache using picasso

first add this to your gradle.

implementation 'com.squareup.picasso:picasso:2.71828'

then you can call the image from your url and store it in your cache.

Picasso.get().load(YOUR_IMAGE_URL).error(R.drawable.error_img).placeholder(R.drawable.loading_image).into(YOUR_IMAGEVIEW);

Picasso will only download the image once, and if you call the same url again it will be redirected to your cache instead of downloading it again(more or less).

note: check the profilier to see the data consumption.

L2_Paver
  • 596
  • 5
  • 11
2

To continue with L2_Paver's answer.

You can also check Glide for the same purpose. You might want to consider the pros and cons of each libraries which serves well for your purpose. This answer might give you some more insights about the comparison. Picasso v/s Imageloader v/s Fresco vs Glide

CSG0811
  • 632
  • 5
  • 20