Android - How to Load Image by name using Glide

我的梦境 提交于 2020-01-01 01:15:13

问题


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.


回答1:


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;
}



回答2:


Try this:

Glide.with(this)
.load(getResources()
.getIdentifier("my_drawable_image_name", "drawable", this.getPackageName())
.into(myImageView);



回答3:


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);


来源:https://stackoverflow.com/questions/42868036/android-how-to-load-image-by-name-using-glide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!