获取从资产或原始文件夹到int数组的多个图像的资源标识符(resId) [英] Get resource identifier (resId) of multiple images from asset or raw folder to an int array

查看:102
本文介绍了获取从资产或原始文件夹到int数组的多个图像的资源标识符(resId)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100多个图像要加载到listView中,并且我正在尝试将所有resId存储到int[]中.我可以使用以下代码在drawable中完成此操作

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

但是我的问题是listView我不需要drawable文件夹中的所有图像.那么我可以在asset中创建一个文件夹并将所有resId放入int[]吗?或者我可以使用raw文件夹吗?任何帮助将不胜感激.

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.

推荐答案

我得到了答案.

是的,我们可以做到.将图像复制到res/raw文件夹中,然后尝试使用此代码

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


        }

此后,resArray包含raw文件夹中的图像的resId

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

这篇关于获取从资产或原始文件夹到int数组的多个图像的资源标识符(resId)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆