java 从Android文件夹中加载所有图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7792942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:28:40  来源:igfitidea点击:

Load all images from folder in Android

javaandroidgridview

提问by omniyo

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 is done in this example: http://developer.android.com/resources/tutorials/views/hello-gridview.htmlBut at runtime and without knowing the names of the files.

事实上,我需要像在这个例子中所做的一样:http: //developer.android.com/resources/tutorials/views/hello-gridview.html但是在运行时并且不知道文件的名称。

Any help?

有什么帮助吗?



After your answers i know how to do it... but i dont know how to go on in the same way as the example:

在您回答之后,我知道该怎么做……但我不知道如何以与示例相同的方式继续:

How could i keep the images in an integer array instead of an drawable array or similar?. Here is my try with drawable: DrawArray[i] = Drawable.createFromPath(fileArray[i].getPath()); But i would like to do it with integer to fit with the link above!

我如何将图像保存在整数数组中而不是可绘制数组或类似数组中?这是我对 drawable 的尝试: DrawArray[i] = Drawable.createFromPath(fileArray[i].getPath()); 但我想用整数来做它以适应上面的链接!

Thanks in advance

提前致谢



GENERAL SOLUTION:http://www.anddev.org/viewtopic.php?t=575

一般解决方案:http ://www.anddev.org/viewtopic.php?t= 575

回答by Wojtek Owczarczyk

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 }
    ....
 }

回答by Lukas Knuth

To get all files of a specific folder, use the list()-methodof the File-class, which lists all Files of the specified directory.

为了得到一个特定的文件夹中的所有文件,使用list()-方法中的File-class,其中列出指定目录中的所有文件。

To create a Bitmap (which can then be drawn), you can use the BitmapFactory-class.

要创建位图(然后可以绘制),您可以使用BitmapFactory-class

After that, you'll need to create your own Adapter (as shown in the linked tutorial) to show your Bitmaps.

之后,您需要创建自己的适配器(如链接教程中所示)来显示您的位图。

回答by Mat Nadrofsky

You can use the Fileclass and it's listFiles() method to return a list of all image files in a given location. From there just take that list and construct your adapter from there.

您可以使用File类及其 listFiles() 方法返回给定位置中所有图像文件的列表。从那里只需获取该列表并从那里构建您的适配器。