求助! 发送图片失败
-
报错:
java.lang.IllegalArgumentException: Unsupported image type (mirai) for ExternalResource net.mamoe.mirai.internal.utils.ExternalResourceImplByFile@e44b38d, considering use gif/png/bmp/jpg format. image header: at net.mamoe.mirai.internal.message.image.ImageDecoderKt.calculateImageInfo(ImageDecoder.kt:159) at net.mamoe.mirai.internal.contact.CommonGroupImpl$uploadImage$lambda-16$$inlined$runBIO$1.invoke(CoroutineUtils.kt:23) at kotlinx.coroutines.InterruptibleKt.runInterruptibleInExpectedContext(Interruptible.kt:51) at kotlinx.coroutines.InterruptibleKt.access$runInterruptibleInExpectedContext(Interruptible.kt:1) at kotlinx.coroutines.InterruptibleKt$runInterruptible$2.invokeSuspend(Interruptible.kt:43) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
代码:
byte[] bytes = Base64.getDecoder().decode(stringR.getData()); String path = dataFile.getPath().substring(0, dataFile.getPath().length() - 9); File file = FileUtils.byte2image(bytes, path , UUID.randomUUID() + ".png"); ExternalResource resource = ExternalResource.create(file); Image image = event.getGroup().uploadImage(resource); MessageChain messageChain = new MessageChainBuilder() .append(new At(event.getSender().getId())) .append("\n") .append(image) .build(); event.getGroup().sendMessage(messageChain);
-
你这明显是上传了不支持的格式
试试用ImageIO转成png格式 -
-
不是,改后缀名是不能改变图片格式的,你得用库转成png才行
-
不是说你把名字改成.png就可以变成png格式的,它里面的图片编码是不会因为改了名字就改变的
-
计算机还没有智能到这个程度
-
我寻思着你都会写Java了,怎么还会干出这事来
-
@起舞弄清影
=。= ,这样就行了吧try { byte[] bytes = Base64.getDecoder().decode(stringR.getData()); String path = dataFile.getPath().substring(0, dataFile.getPath().length() - 9); File file = new File(path + "serverInfo.png"); ImageIO.write(ImageIO.read(new ByteArrayInputStream(bytes)), "png", new File(file.getPath())); ExternalResource resource = ExternalResource.create(file); Image image = event.getGroup().uploadImage(resource); MessageChain messageChain = new MessageChainBuilder() .append(new At(event.getSender().getId())) .append("\n") .append(image) .build(); event.getGroup().sendMessage(messageChain); }catch (Exception e){ e.printStackTrace(); MessageUtil.sendGroupMessage(event, "获取图片失败!"); }
-
@Danile0 第5行多了个new FIle(); 忽略就行
-
considering use gif/png/bmp/jpg format. image header:
如果这个就是完整的日志,
header:
后面没有其他东西
那么说明你传了一个空文件 -
@Danile0 对