Load all images from folder in Android

前端 未结 3 1267
有刺的猬
有刺的猬 2021-01-15 16:50

I would like to load all images in a folder without knowing the names of the files and store them into a Integer vector - at runtime.

In fact, i need the same as it

3条回答
  •  一向
    一向 (楼主)
    2021-01-15 17:18

    Using some of the code from the following topic:

    Load and display all the images from a folder

    You can use a standard way, such as:

    // array of supported extensions (use a List if you prefer)
        static final String[] EXTENSIONS = new String[]{
            "gif", "png", "bmp" // and other formats you need
        };
        // filter to identify images based on their extensions
       static final FilenameFilter IMAGE_FILTER = new FilenameFilter() {
    
        @Override
        public boolean accept(final File dir, final String name) {
            for (final String ext : EXTENSIONS) {
                if (name.endsWith("." + ext)) {
                    return (true);
                }
            }
            return (false);
        }
       };
    

    and then use is it as so:

    {
        ....
        File dir = new File(dirPath);
        File[] filelist = dir.listFiles(IMAGE_FILTER );
        for (File f : filelist)
        { // do your stuff here }
        ....
     }
    

提交回复
热议问题