How to read an image from file or URL

The “javax.imageio” package is used to deal with the Java image stuff. Here’s two “ImageIO” code snippet to read an image file.

1. Read from local file


File sourceimage = new File("c:\\mypic.jpg");
Image image = ImageIO.read(sourceimage);

2. Read from URL


URL url = new URL("https://mkyong.com/image/mypic.jpg");
Image image = ImageIO.read(url);

ImageIO Example

In this example, you will use ImageIO to read a file from an URL and display it in a frame.


package com.mkyong.image;

import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ReadImage 
{	
    public static void main( String[] args )
    {
    	Image image = null;
        try {
            URL url = new URL("https://mkyong.com/image/mypic.jpg");
            image = ImageIO.read(url);
        } catch (IOException e) {
        	e.printStackTrace();
        }
        
        JFrame frame = new JFrame();
        frame.setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        frame.add(label);
        frame.setVisible(true);
    }
}

Output…

read-image-from-url

24 comments on “How to read an image from file or URL

  1. I also tried but showing 403 error. How it can be resolve

    Reply
  2. Nice sample…!! what about take screenshot from url instead download image…!!! for example, screenshot of Google earth…!!!

    Reply
  3. saved the day in a small console app i’m just running.
    thank you

    Reply
  4. Excelente, solo revisar que la url de la imagen esta activo y que la imagen exista

    Reply
  5. Preciso e direto, realmente salvou o meu dia, muito obrigado pelo conteúdo!

    Reply
  6. hi Can any help me out how to convert local path image to url formate.

    Reply
  7. Hey buddy, I wanted to ask you something. I’m working on a program that brings screenshots from a security camera, and it worked very well, but I wanted to add a reload method to keep getting the screenshots from the ip camera until the user closes the frame. Thanks in advance.

    Reply
  8. URL url = new URL(“https://mkyong.com/image/mypic.jpg”);
    image = ImageIO.read(url);
    will this work for https://

    Reply
  9. private String imagedir = “C:\”;
    Not working neither

    Reply
  10. Hi, i need help, i am so desperate.
    I am trying to show and image which is out of the project. Using a path like:

    c:\mypic.jpg is not working for me.
    My program is kind of:
    private String imagedir = “C://”;
    private String imageFileNames = “s02a01.jpg”;
    ImageIcon icon;
    icon = createImageIcon(imagedir + imageFileNames);

    Can someone help me with the correct imagedir?

    Reply
    1. C:\mypic.jpg is where the image mypic with the extension of a JPEG is stored, change “C:\mypic.jpg” to the exact location of your picture

      Reply
  11. I am developing an web application with Struts2 and jsp. I have an issue
    to store images. I am storing images in folder and their relative
    path’s in mysql database.

    when I retrieve path from database then using tag i have displayed image like:

    <img src="” alt=” height=400 weight=100>

    It is working fine with eclipse default browser but not working (that is Not displaying image) in chrome/mozilla.

    when im keep this

    not working

    Reply
  12. How to read text from image? I want to extract text from image, for example I have passport, from that i want names and all information and image.

    Reply
  13. Mkyong, I’m making an application that’s requiring you to pretty much press a JButton to load information from a JSon page and needless to say I need it to load an image too.. I can’t figure out the button code.. Could you please help me?

    Reply
  14. Great tutorial, really clear and to the point. Keep it up 😀

    Reply
  15. I tested your code, and I keep getting javax.imageio.IIOException: Can’t get input stream from URL! error

    Reply
  16. hi sir, iam ramesh thank u very much given this code i am doing project on visual cryptography i want to do the overlap with another share so for that one what i have to do
    File sourceimage = new File(“commonshare.png”); this image is visible now we browse the another image and it is overlap with the common share.
    what should i do sir plz help me plz help me sir

    Reply
  17. I am getting error in the line
    ” Image image = ImageIO.read(sourceimage); ”
    saying
    ” Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor ” what is the solution for this.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *