dynamically getting all image resource id in an array

前端 未结 2 2020
陌清茗
陌清茗 2020-12-09 11:52

there are many images in drawable foder so instead manually creating array of all image resource ids , i want to get all images dynamically of drawable folder in array. cur

2条回答
  •  醉话见心
    2020-12-09 11:55

    Well, If your image names are img1, img2 and so on, then you can create a variable like

    String url = "drawable/"+"img"+i;
    
    int imageKey = getResources().getIdentifier(url, "drawable", getPackageName());
    

    you can also replace your getPackageName() method by your package name like "com.android.resource"
    Simply,the general function is

    public int getIdentifier(String name, String defType, String defPackage)
    

提交回复
热议问题