【问题标题】:ImageIO Can't read input file (Kotlin)ImageIO 无法读取输入文件 (Kotlin)
【发布时间】:2018-03-14 03:53:09
【问题描述】:

您好,我使用 kotlin 制作了一个不和谐的机器人,我有一个名为 sam.png 的 PNG 但我尝试插入并收到此错误:

[23:18:38] [致命] [JDA]: javax.imageio.IIOException: 无法读取输入 文件!

override fun run(ev: MessageReceivedEvent) {
    var selaSam: Image = ImageIO.read(File("/sam.png"))
    var image = LikeUtils.downloadImage(LikeUtils.getUserHandle(ev).getEffectiveAvatarUrl())

    image.graphics.drawImage(selaSam, 200, 200, null)

    LikeUtils.sendFile(image, "/resources/sam.png", null)
}

【问题讨论】:

    标签: java kotlin discord


    【解决方案1】:

    路径/sam.png 表示该文件在根路径中可用(不是项目的工作路径,而是系统的根路径)。我怀疑情况是否如此。

    我猜你想要那条路径:./sam.png。 最好用

    检查文件是否存在
    override fun run(ev: MessageReceivedEvent) {
        val imagePath = "/sam.png"
        val image = File(imagePath)
        if (!image.exists()) throw RuntimeException("Image $imagePath not found")
        if (image.isDirectory) throw RuntimeException("Image $imagePath is a directory")
    
        var selaSam: Image = ImageIO.read(image)
        var image = LikeUtils.downloadImage(LikeUtils.getUserHandle(ev).getEffectiveAvatarUrl())
    
        image.graphics.drawImage(selaSam, 200, 200, null)
    
        LikeUtils.sendFile(image, "/resources/sam.png", null)
    }
    

    【讨论】:

    • 还是报错,我放到resources文件夹下,路径是怎样的?
    • 还是同样的错误,还是你现在得到RuntimeException,因为图像不存在?
    • RuntimeException
    • 如果文件存储在项目的主目录中,只需删除标题/。要知道你在哪里,你可以随时打印出println(File(".").absolutePath)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多