omniyo
omniyo

Reputation: 340

Load all images from folder in Android

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.html But at runtime and without knowing the names of the files.

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!

Thanks in advance


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

Upvotes: 2

Views: 14033

Answers (3)

Wojciech Owczarczyk
Wojciech Owczarczyk

Reputation: 5735

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

Upvotes: 9

Mat Nadrofsky
Mat Nadrofsky

Reputation: 8264

You can use the File class 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.

Upvotes: 1

Lukas Knuth
Lukas Knuth

Reputation: 25755

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

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

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

Upvotes: 5

Related Questions