java - 尝试使用 ImageIO.read 加载图像时出错

标签 java google-maps

搜索类似问题后,我想问以下问题:

我试图从 Google 静态 map 加载图片,但收到此错误:线程“main”java.lang.IllegalArgumentException 中出现异常:输入 == null! 在 javax.imageio.ImageIO.read(来源未知) 在 MapPicturePanel.main(MapPicturePanel.java:18)

以下是该类的完整代码:

 import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MapPicturePanel {
    private JScrollPane getContent(BufferedImage image) {
        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        label.setHorizontalAlignment(JLabel.CENTER);
        return new JScrollPane(label);
    }

    public static void main(String[] args) throws IOException {
        String path = "http://maps.googleapis.com/maps/api/staticmap?center=37.426616,-122.176380&zoom=14&size=800x600&scale=2&format=jpg&maptype=hybrid&sensor=false";
        URL url = MapPicturePanel.class.getResource(path);
        BufferedImage image = ImageIO.read(url);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new MapPicturePanel().getContent(image));
        f.setSize(800,800);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

最佳答案

您正在尝试在类路径中查找资源(这就是 Class.getResource 所做的)。但看起来您想从外部 url 加载图像。

就这样做

BufferedImage image  - ImageIO.read(new URL(path));

关于java - 尝试使用 ImageIO.read 加载图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8460764/

相关文章:

java - 数据报 channel 套接字不编写 Java

JavaFX VBox getChildren().addAll() 错误

java - Spring Framework MVC 中的 "Webpage has expired"

Java synchronized() 关键字——防止字段中引用的对象发生变化?

android - Geocoder.getFromLocationName 不适用于某些地方

安卓 : Google Maps v2 in Asus Nexus 7 is not working

java - 使用eclipse查找jboss中的内存泄漏

google-maps - Google Maps API 获取公交路线

ruby - 我如何获得静态谷歌地图的边界?

javascript - 将自定义控件添加到作为下拉列表的 Google map