0

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:

Resource File

Toast message shows that it pulls the information from the string-array fine however the referencing doesnt seem to work.

enter image description here

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Tim Hannah
  • 172
  • 2
  • 4
  • 13
  • There can be a huge amount of reasons why it does't work. I'd recommended you use Option 2. It easy and quickly. – sergkk Jun 05 '20 at 21:03
  • @sergkk thats what I was going with but its not working properly – Tim Hannah Jun 05 '20 at 21:06
  • There a lot of ways to implement Option 2.Try this simple way: 1. Add images to folder "drawable" 2. Create mapper like fun (position:int): if (position ==1) return R.drawable.your_photo_name 3. https://stackoverflow.com/questions/42868036/android-how-to-load-image-by-name-using-glide – sergkk Jun 05 '20 at 21:17
  • So i already have an adapter that stores the string for the image in value "imageGallery" so that be is already being handled. When i click on the one i changed it displays the correct text in toast when the toast is set to display the value on imageGallery however for some reason it doesn't work as a link – Tim Hannah Jun 05 '20 at 21:21
  • Like i say it all works if i leave it with URLs in the string-array however if i change it from a url to a local resource path it doesnt load. litterally replace `http://britishsheepbreeds.co.uk/app/gallery/beltex.jpg` for `R.mipmap.beltex` makes it not work. even though the image is there in that location. – Tim Hannah Jun 05 '20 at 21:31

1 Answers1

1

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);
  • Worked like a charm, I was sure I had to change the string to a resource reference thing. Even tried getResource but it kept throwing an error because I wasn't using the correct syntax. Thanks. – Tim Hannah Jun 06 '20 at 09:35
  • Quick follow up. I use glide to push images to my Recyclerview as well. The Glide .load is slight different because i have to work out the position of the item before showing the image. How can i use the get resource there?? code is `.load(imagesArrayList.get(position).getmImages())` – Tim Hannah Jun 06 '20 at 10:18
  • you can use holder.itemview.context.getresources() or pass your activity to the adapter and use activity.getresources() – Narendra Bhatt Jun 06 '20 at 16:57