简体   繁体   中英

How to check if a BufferedImage is complete in java?

I'm trying to load an image from a server, with the following code:

BufferedImage img;    
do{
    try {
        URL url = new URL(myUrl);
        img = ImageIO.read(url);
    }catch (IOException e){}
}while(img == null);

The idea is to keep checking source until the image is found, because it will be uploaded while the code is still executing. The problem is that when the image is still being loaded to the server the code stops, showing a fraction of the image, because it is no longer null but it's still not complete, so how can I check if the image is complete?

From within an Applet I handle image loading like this:

MediaTracker mediaTracker = new MediaTracker(this);
Image image = getImage(getDocumentBase(), "image-filename.png");
mediaTracker.addImage(image, 0);
try {
    mediaTracker.waitForAll();
} catch (InterruptedException interuptedException) {
    interuptedException.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM