在Android中加载文件夹中的所有图像

omn*_*iyo 2 java android gridview

我想在不知道文件名的情况下将所有图像加载到文件夹中,并将它们存储到整数向量中 - 在运行时.

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

有帮助吗?


在你的答案之后我知道该怎么做...但我不知道如何以与例子相同的方式继续下去:

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

提前致谢


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

Woj*_*zyk 9

使用以下主题中的一些代码:

加载并显示文件夹中的所有图像

您可以使用标准方式,例如:

// 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);
    }
   };
Run Code Online (Sandbox Code Playgroud)

然后使用就是这样:

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


Luk*_*uth 5

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

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

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