Get resource identifier (resId) of multiple images from asset or raw folder to an int array

。_饼干妹妹 提交于 2019-12-11 13:12:41

问题


I have 100+ images to load into a listView, and I am trying to store all the resId into an int[]. I can done it from drawable by using the below code

Field[] ID_Fields = R.drawable.class.getFields();
        resArray = new int[ID_Fields.length];
        for(int i = 0; i < ID_Fields.length; i++)
        {
            try {

                resArray[i] = ID_Fields[i].getInt(null);

            } catch (IllegalArgumentException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

But my problem is that I don't need all images in the drawable folder for my listView. So can i create a folder inside the asset and get all resId into a int[] ? or can I use raw folder for this? Any help will be appreciated.


回答1:


I got the answer.

YES we can do it. Copy the images into res/raw folder and try with this code

Field[] ID_Fields = R.raw.class.getFields();
        resArray = new int[ID_Fields.length];
        for(int i = 0; i < ID_Fields.length; i++)
        {

            try {
                resArray[i]= ID_Fields[i].getInt(null);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

After that resArray contains the resId of the images in the raw folder




回答2:


It is possible..but It is not a good way for you. You can put images in Asset folder Like this See Here

but if you do this there can be issues created Issues

So now it is up to you how you use it. :)



来源:https://stackoverflow.com/questions/16433093/get-resource-identifier-resid-of-multiple-images-from-asset-or-raw-folder-to-a

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