简体   繁体   中英

Android set dynamic image on Imagebutton

I want to set image dynamically on the ImageButton in android. However, i have more than 200 images. what would be the good solution for that?

The best idea is that i can use the name of image to call different images. ie imagebutton.setImage("/res/abc.png"); however, it seems to me that it is not trivial to do so.. please help me to solve this problems. thanks a lot!!

regards penny

You can take the images in your resource folder. After that follow this simple code:

try {               
                Class<drawable> res = R.drawable.class;
                if(str!=null){                                   
                Field field = res.getField(str);
                int drawableId = field.getInt(null);
                bengalidaypng.setImageResource(drawableId);              
                }
            }
            catch (Exception e) {
                      System.out.println("Image not found in drawable folder");
            } 

A more detailed sample can be found here .

You could use Typed Array resource. There is an example at the end of the link how to use it for drawables (images).

Edited:

Resources can be accessed as raw data: use AssetManager.open(..) Then you can use BitmapFactory.decodeStream(..) to create a Bitmap from the data stream.

您可以将图像存储到数据库中,然后绑定每一行时将图像拉入ListView中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM