上传照片时一般会对其进行压缩等处理,当将手机本地照片decode为bitmap后,bitmap对象中是没有这Exif信息的,所以需将原图的Exif信息写入新的处理过的照片中
public static void saveExif(String oldFilePath, String newFilePath) throws Exception {
ExifInterface oldExif = new ExifInterface(oldFilePath);
ExifInterface newExif = new ExifInterface(newFilePath);
Class<ExifInterface> cls = ExifInterface.class;
Field[] fields = cls.getFields();
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
if (!TextUtils.isEmpty(fieldName) && fieldName.startsWith("TAG")) {
String fieldValue = fields[i].get(cls).toString();
String attribute = oldExif.getAttribute(fieldValue);
if (attribute != null) {
newExif.setAttribute(fieldValue, attribute);
}
}
}
newExif.saveAttributes();
}
注:处理后的图片必须是JPEG格式的,只有JPEG格式图片支持写入Exif