Attempts to load the image from file path - Java 2D Graphics

Java examples for 2D Graphics:Image File

Description

Attempts to load the image from file path

Demo Code


//package com.java2s;
import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    /**/*from   w  ww.  j  a va2 s .  c om*/
     * Attempts to load the image
     * @return
     * Returns True if it is successful, returns false if it is not.
     */
    public static BufferedImage loadImage(String filePath) {
        BufferedImage sourceImage;
        sourceImage = null;
        try {
            if (filePath != null) {
                File file = new File(filePath);
                if (file.isFile()) {
                    sourceImage = ImageIO.read(file);
                } else {
                    System.out.println("File Not found");
                }
            }
            return sourceImage; // If the file path is empty, it isn't successful.

        } catch (IOException ex) {
            ex.printStackTrace();
            return sourceImage;
        }
    }
}

Related Tutorials