今天我们测试出了一个小问题,在从相册选择图片时,没有判断此图片是否破损。在此记录一下判断方法:
private static boolean imageBroken(String filePath) {
BitmapFactory.Options options = null;
if (options == null) options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options);
if (options.mCancel || options.outWidth <= 0 || options.outHeight <= 0) {
//表示图片已损毁 return true;
}
if (filePath.endsWith(".gif")) {
//针对gif进行判断 try {
GifDrawable gifDrawable = new GifDrawable(new File(filePath));
if (gifDrawable != null) {
if (gifDrawable.getError() != GifError.NO_ERROR) {
return true;
}
gifDrawable.recycle();
} else {
return true;
}
} catch (IOException e) {
e.printStackTrace();
return true;
}
}
return false;
}
网友评论