知道,如果一个文件是Java / Android的一个形象形象、文件、Java、Android

2023-09-05 03:33:01 作者:裳舞红雪

由于文件路径是什么知道的最简单的方法,如果该文件是Java(Android的)一个图像(任何种类)?谢谢

Given a file path what is the easiest way of know if that file is a image (of any kind) in Java(Android)? Thanks

推荐答案

有外部工具,可以帮助你做到这一点。请查看 JMimeMagic 和的 JMagick 。您也可以尝试使用的ImageIO 类读取该文件,但可能是昂贵的,而不是完全可靠。

There are external tools that can help you do this. Check out JMimeMagic and JMagick. You can also attempt to read the file using the ImageIO class, but that can be costly and is not entirely foolproof.

BufferedImage image = ImageIO.read(new FileInputStream(new File(..._)));

这个问题已经被问上这么几次。看到这些额外的线程在同一主题:

This question has been asked on SO a couple of times. See these additional threads on the same topic:

检查文件是否

How来检查上传的文件是否是一个图像或其他文件?