简体   繁体   English

错误时从URL加载图像和默认图像

[英]Load image from URL and default image on error

I am trying to load an image from a URL and this was very easy and works great. 我正在尝试从URL加载图像,这非常简单并且效果很好。 However, if the image doesnt exist I want it to default to a local image. 但是,如果图像不存在,我希望它默认为本地图像。 The problem I am having is that it doesnt throw any exceptions. 我遇到的问题是它不会引发任何异常。 Instead it will just not display anything. 相反,它将不会显示任何内容。 I'm assuming I have to validate the URL some how, but I want to also be sure that its an image and not a webpage/script...etc 我假设我必须以某种方式验证URL,但是我还要确保它是图像而不是网页/脚本...等

Here is my basic test code. 这是我的基本测试代码。 This works: 这有效:

public class DownloadImage extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox();

        String imageSource = "https://www.wbkidsgo.com/Portals/1/Content/Hero/HeroBugsCarrot/Source/Default/WB_LT_HeroImage_Bugs_v1.png";

        ImageView imageView = new ImageView(new Image(imageSource));

        root.getChildren().add(imageView);

        primaryStage.setScene(new Scene(root, 1000, 1000));
        primaryStage.show();

    }
}

But if I load a bad URL ie (two s's in Portals): 但是,如果我加载了错误的URL,即(Portal中为2):

String imageSource = "https://www.wbkidsgo.com/Portalss/1/Content/Hero/HeroBugsCarrot/Source/Default/WB_LT_HeroImage_Bugs_v1.png";

It will just be blank. 它只是空白。 I thought about creating an HTTP client and sending a request prior to creating the image, but generating the HTTP client takes a few seconds and I will be loading upwards of 300 or so images. 我曾考虑过在创建映像之前创建HTTP客户端并发送请求,但是生成HTTP客户端需要花费几秒钟的时间,而我将最多加载300个左右的图像。 I guess I can have one http client, and make one request for each image and check the response datatype to see if its an image, but is there a better way? 我想我可以有一个http客户端,并对每个图像发出一个请求,然后检查响应数据类型以查看其是否为图像,但是还有更好的方法吗?

You can use the errorProperty and exceptionProperty to check the error status of the image: 您可以使用errorPropertyexceptionProperty来检查图像的错误状态:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class ImageTest extends Application {

    public static void main(String[] args) {
        launch(args) ;
    }

    @Override
    public void start(Stage primaryStage) {
        String[] urls = new String[]{
                "https://www.wbkidsgo.com/Portals/1/Content/Hero/HeroBugsCarrot/Source/Default/WB_LT_HeroImage_Bugs_v1.png",
                "https://www.wbkidsgo.com/Portalss/1/Content/Hero/HeroBugsCarrot/Source/Default/WB_LT_HeroImage_Bugs_v1.png"
        } ;

        for (String url : urls) {
            Image image = new Image(url);
            if (image.isError()) {
                System.out.println("Error loading image from "+url);
                // if you need more details
                // image.getException().printStackTrace();
            } else {
                System.out.println("Successfully loaded image from " + url);
            }
        }
        Platform.exit();
    }

}

Which gives the following output: 给出以下输出:

Successfully loaded image from https://www.wbkidsgo.com/Portals/1/Content/Hero/HeroBugsCarrot/Source/Default/WB_LT_HeroImage_Bugs_v1.png
Error loading image from https://www.wbkidsgo.com/Portalss/1/Content/Hero/HeroBugsCarrot/Source/Default/WB_LT_HeroImage_Bugs_v1.png

You can use the ImageView.setImage() method to change the image. 您可以使用ImageView.setImage()方法更改图像。 I recommend initializing the ImageView using your default image in the form of a BufferedImage , then setting it later with your actual image. 我建议使用您的默认图像以BufferedImage的形式初始化ImageView ,然后在以后使用实际图像进行设置。 You can load this from your filesystem or classpath using ImageIO . 您可以使用ImageIO从文件系统或类路径中加载它。

As for the actual image, I would also use ImageIO to load the url as a BufferedImage . 至于实际的图像,我还将使用ImageIO将URL加载为BufferedImage This throws an IOException , so if it errors or does not link to a supported image type, nothing will happen. 这将引发IOException ,因此,如果发生错误或未链接到受支持的图像类型,则不会发生任何事情。

If you know much about threads and concurrency, I would load the image on another thread so it doesn't freeze your main thread. 如果您对线程和并发性了解很多,我会将图像加载到另一个线程上,这样就不会冻结您的主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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