如何获取资源文件的扩展名?

How to get the extension of Files of resources?

在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

我回答我的问题。

我将所有图像文件移到了 "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);
        }