获取从资产或原始文件夹到int数组的多个图像的资源标识符(resId) [英] Get resource identifier (resId) of multiple images from asset or raw folder to an int array
问题描述
我有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屋!