在android中,我需要从res / drawable目录中加载一组图像
我使用以下代码:
Field[] fields = R.drawable.class.getFields();
List<Picture> pictures = new ArrayList<>();
for (Field field : fields) {
String name = field.getName();
if (name.startsWith("img")) {
Picture picture = new Picture(name);
pictures.add(picture);
}
}
在'drawable'中的所有文件中,它找到所有以'img'字符串开头的文件。 为了使此代码正常工作,我必须自己手动更改图像文件的名称。 如果我可以在drawable中获得每个资源的扩展名(例如jpg,png等),则无需更改文件名。
有什么办法可以做到这一点?
感谢您的帮助,谢谢:D
答案 0 :(得分:0)
我回答了我的问题。
我将所有图像文件都移到了“ asset / imgs”中 并使用以下代码加载它们
List<Picture> pictures = new ArrayList<>();
try {
String[] images = assetManager.list("imgs");
for (String name : images) {
Picture picture = new Picture(name);
pictures.add(picture);
}
} catch (IOException e) {
// you can print error or log.
throw new RuntimeException(e);
}