我如何检查一个文件,如果它是一个图像?如下所示:
如果标准库不可行,我怎么用MagickImage lib来做到这一点?
试试这个代码吧:
public class ImageFileFilter implements FileFilter
{
File file;
private final String[] okFileExtensions = new String[] {"jpg", "png", "gif","jpeg"};
/**
*
*/
public ImageFileFilter(File newfile)
{
this.file=newfile;
}
public boolean accept(File file)
{
for (String extension : okFileExtensions)
{
if (file.getName().toLowerCase().endsWith(extension))
{
return true;
}
}
return false;
}
}
你可以尝试按如下方式通过BitmapFactory解析文件:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
if (options.outWidth != -1 && options.outHeight != -1) {
// This is an image file.
}
else {
// This is not an image file.
}