Currently I have an app that uses a RecyclerView
, and when one of the items are clicked it loads the image using Glide. These are all hosted on a webserver and the URL's for all the items are stored in a String-Array. The adapter works out which position matches to what and is stored in 'imageGallery
' downloads the image and then loads it in the app. once its downloaded for the first time its stored and it reuses that each time you press the item however each new item you press it has to download periodically.
This is an example of the string-array soring the URL's
<string-array name="gallery">
<item>http://britishsheepbreeds.co.uk/app/gallery/badger_welsh_mountain.jpg</item>
<item>http://britishsheepbreeds.co.uk/app/gallery/balwen.jpg</item>
<item>http://britishsheepbreeds.co.uk/app/gallery/beltex.jpg</item>
</string-array>
After beta testing this with some people it was deemed that having the images online and not loaded into the app until the item is clicked was not a good method so I was thinking of two options.
Option 1. download all images when the app is first loaded the first time then use the cached/images on storage to load in future.
Option 2. have all the images local in drawable or mipmap (or another directory in the app) so they call them from the app storage rather than relying on the internet.
I have tried to do this by amending my strings however this isn't working
I changed one item in the String-Array to test with and added a toast to make sure that it was calling the right bit of information however its still not loading.
<string-array name="gallery">
<item>http://britishsheepbreeds.co.uk/app/gallery/badger_welsh_mountain.jpg</item>
<item>http://britishsheepbreeds.co.uk/app/gallery/balwen.jpg</item>
<item>R.mipmap.beltex</item>
</string-array>
Glide code is:
Glide.with(this)
.asBitmap()
.load(imageGallery)
.into(gallery);
Resource folder:
Toast message shows that it pulls the information from the string-array fine however the referencing doesnt seem to work.
if I replace .load(imageGallery)
with .load(R.mipmap.beltex)
the image loads for all items.
Any help is welcome. What am I doing wrong? Its almost like the referencing doesnt work from a string. do I have to parse the string to make it work as a reference field.
You need to get the id of that image from the name in order to load it.
Replace the array item from R.mipmap.beltex to beltex and while setting the image get id from the name
int resId = getResources().getIdentifier(imageGallery, "mipmap",getPackageName());
Glide.with(this).load(resId).into(gallery);