Hi everyone,

I'm having a little problem I'm sure someone can help me with. I'm trying to read in 10 png images and save them in an ArrayList as BufferedImage objects (I think this is the right way to go about it but if not plaease say so).

Here is my code so far:

package imageloader;

import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.util.ArrayList;

public class Interface 
{

    /* Fields */

    private ArrayList<String> fileList = new ArrayList<String>();
    private ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();

    
    /* Constructor */

    public Interface()
    {
        buildFileList();
        loadImages();
    }

    /**
     * Method to populate the fileList array.
     */

    private void buildFileList()
    {
        fileList.add( "./images/image1.png" );
        fileList.add( "./images/image2.png" );
        fileList.add( "./images/image3.png" );
        fileList.add( "./images/image4.png" );
        fileList.add( "./images/image5.png" );
        fileList.add( "./images/image6.png" );
        fileList.add( "./images/image7.png" );
        fileList.add( "./images/image8.png" );
        fileList.add( "./images/image9.png" );
        fileList.add( "./images/image10.png" );
    }

    /**
     * Method to load images from file and store internally.
     */

    private void loadImages()
    {
        BufferedImage temp = null;

        try
        {
            for( String item : fileList )
            {
                temp = ImageIO.read( new File( item ) );
                imageList.add( temp );
            }
        }

        catch( IOException e )
        {
            System.err.println( e.getMessage() );
        }
    }
}

I'm trying to read all files in and store them all in an ArrayList as BufferedImage objects, but I'm getting a "Can't read input file!" error message and I'm not sure what I'm doing wrong here. The paths are all okay. Any suggestions?

Thanks!

Please copy and paste here full text of the error message.

Where is the images folder relative to the current directory when the program is executed?
For debugging, Create the File object outside the ImageIO.read call and display its full path

Before passing in the File object to the ImageIO#read method, print out its absolute path (File#getAbsolutePath)and verify whether it is the same as the path where you have stored your images.

Edit: Oops, didn't see Norm1's reply. But yes, print out the full path using the getAbsolutePath()

What you do depends on if this is a stand alone or applet. if it's a stand alone i find something like this works.

URL myurl = this.getClass().getResource( "images/image1.png");
Image myimage =Toolkit.getDefaultToolkit().getImage(myurl);

Also if you have an image, why not use an image rather than a buffered image. I sort of thought buffered images were for drawing your own images.

Mike

Thanks everyone for your advice.

@ Norm1 and SOS : Yep, I got the full path using getAbsolutePath() and it turned out it was wrong.

@ Mike : Thanks for the tip about using an Image object instead of a BufferedImage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.