34

Hi I'm 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.

Lino
  • 5,084
  • 3
  • 21
  • 39
James
  • 1,190
  • 5
  • 27
  • 52

7 Answers7

51

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;
}
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
12

Try this:

Glide.with(this)
.load(getResources()
.getIdentifier("my_drawable_image_name", "drawable", this.getPackageName())
.into(myImageView);
Abhay Tomar
  • 175
  • 1
  • 9
Hamlet Mendez
  • 153
  • 1
  • 8
5

Use Uri.

String image = "image.jpg";
    String completePath = Environment.getExternalStorageDirectory() + "/" + image;

    File file = new File(completePath);
    Uri uri = Uri.fromFile(file);

    Glide.with(this).load(uri).into(imageView);
josedlujan
  • 5,357
  • 2
  • 27
  • 49
0

You can use the following code to get Bitmap, Drawable from URL using Glide. This code snippet is used to set drawableEnd/drawbleRight for TextView.

Glide.with(context)
            .asBitmap()
            .load(url)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    bitmap: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {
                    val drawable: Drawable = BitmapDrawable(
                        context.resources,
                        bitmap
                    )

                    textView.setCompoundDrawablesWithIntrinsicBounds(
                        null, null, drawable, null
                    )
                }

                override fun onLoadCleared(placeholder: Drawable?) {

                }
            })
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0
Glide.with(ProfileActivity.this)
        .load(profileImage)
        .placeholder(R.drawable.baseline_person_24)
        .into(binding.profileIv);
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 2
    Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? **If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient.** Can you kindly [edit] your answer to offer an explanation? – Jeremy Caney Aug 30 '23 at 00:19
-1

I think you should try this

Glide.with(mContext)
            .load(mContext.getResources().getDrawable(R.drawable.my_drawable_image_name))
            .placeholder(R.mipmap.icon)
            .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. I wanted rounded-corner of image and that's why I used Glide. In your case, You should use this.

myImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.my_drawable_image_name))

If you also want rounded image use this.

Glide.with(mContext)
            .load(Glide.with(mContext)
            .transform(new FitCenter(), new RoundedCorners((int) mContext.getResources().getDimension(R.dimen._10sdp))
            .placeholder(R.mipmap.icon)
            .into(myImageView);
Nevil Ghelani
  • 687
  • 10
  • 10
-1

Easy way to load image from drawable by using Name

Glide.with(context)
            .load(context.getResources().getIdentifier("my_drawable_image_name", "drawable", context.getPackageName()))
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .placeholder(R.drawable.add_photo_placeholder)
            .error(R.color.red)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .into(userPhoto);
Mujahid Khan
  • 1,712
  • 1
  • 18
  • 24