Android - How to Load Image by name using Glide

2020-05-24 20:55发布

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.

3条回答
我想做一个坏孩纸
2楼-- · 2020-05-24 21:21

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);
查看更多
闹够了就滚
3楼-- · 2020-05-24 21:34

Try this:

Glide.with(this)
.load(getResources()
.getIdentifier("my_drawable_image_name", "drawable", this.getPackageName())
.into(myImageView);
查看更多
做个烂人
4楼-- · 2020-05-24 21:35

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;
}
查看更多
登录 后发表回答