Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to Load Image by name using Glide

Hi im using Glide to load image from my drawable folder and all works fine, this is my code :

Glide.with(this).load(R.drawable.my_drawable_image_name).into(myImageView); 

i'm wondering if there is a way to load the image just by name , something like :

Glide.with(this).load(my_drawable_image_name).into(myImageView); 

because i want to get the image name dynamically ,for example from a database...

do you have any suggestion about that? thanks in advance.

like image 968
James Avatar asked Mar 17 '17 22:03

James


People also ask

How do you load a drawable image with Glide?

into(myImageView); Load method of Glide also accept drawable object as a parameter to load image. You just have to get the drawable object from your drawable image and pass it to the load method.

What is Glide in Android?

Before getting into Glide example, we should know what is glide, Glide is an image processing library developed by muyangmin. Using glide library we can show image, decode images,cache images, animated gifs and many more. This example demonstrate about how to integrate glide in android.

How to integrate glide library in Android Studio?

Using glide library we can show image, decode images,cache images, animated gifs and many more. This example demonstrate about how to integrate glide in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Do you use glide for image loading?

Many of us use glide for image loading but only some of us know its real power. If we dive into the features of glide, the article will go into TL;DR category.

What is the difference between Glide and other image libraries?

You can read some comparison here and here. Glide offers some additional advantages over other image loading libraries. For example, when loading an image, Glide will cache both a version of the image in its original size as well as one that’s the size of its intended ImageView.


2 Answers

Call getImage method for get Drawable using Name only.

Glide.with(this).load(getImage(my_drawable_image_name)).into(myImageView);  public int getImage(String imageName) {      int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());      return drawableResourceId; } 
like image 182
Narendra Sorathiya Avatar answered Sep 17 '22 13:09

Narendra Sorathiya


Try this:

Glide.with(this) .load(getResources() .getIdentifier("my_drawable_image_name", "drawable", this.getPackageName()) .into(myImageView); 
like image 26
Hamlet Mendez Avatar answered Sep 16 '22 13:09

Hamlet Mendez