有没有一种通用的方法来知道文件是否是受支持的图像类型?

| 有没有一种方法可以通用地知道文件是否是受支持的图像类型?当然,我可以查看文件扩展名,并将其与一组已知的字符串进行比较,但是是否有更通用的“ android \”方法呢? 真的,我想知道我是否可以解码和显示图像。     
已邀请:
        其实有更好的方法(尽管仍然使用BitmapFactory)。您可以使用相同的工厂,但不能读取位图本身(当然这要快得多)。有一个带有Options参数的解码文件版本,您可以指定它只读取位图大小(当然是类型),而不是位图本身:
String filePath;// assign the file to the path
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap img = BitmapFactory.decodeFile(filePath, options);
if (img == null) {
  // invalid image file
}
else {
  // valid image file
}
    
        最简单(也是最可靠)的方法是尝试将其打开并解码为
Bitmap
-类似:
String filePath;// assign the file to the path
Bitmap img = BitmapFactory.decodeFile(filePath);
if (img == null) {
  // invalid image file
}
else {
  // valid image file
}
虽然不漂亮,但是可以。     

要回复问题请先登录注册