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

问题描述 投票:0回答:1

在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

android android-drawable
1个回答
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);
        }
© www.soinside.com 2019 - 2024. All rights reserved.