Taki
Taki

Reputation: 3730

Storing images from api into room db

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.

Upvotes: 2

Views: 859

Answers (2)

CSG0811
CSG0811

Reputation: 643

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

Upvotes: 2

L2_Paver
L2_Paver

Reputation: 626

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.

Upvotes: 2

Related Questions