ExifInterface接口setAttribute()设置ExposureTime丢失精度
最近需要写入 曝光时间 到exif中,使用ExifInterface.setAttribute(ExifInterface.TAG_EXPOSURE_TIME,mExposureTime)
当我传入的值为 1/4000 的时候,这个原生接口导致精度丢失,这是原生接口代码
public void setAttribute(@NonNull String tag, @Nullable String value) {
if (tag == null) {
throw new NullPointerException("tag shouldn't be null");
}
// Convert the given value to rational values for backwards compatibility.
if (value != null && sTagSetForCompatibility.contains(tag)) {
if (tag.equals(TAG_GPS_TIMESTAMP)) {
Matcher m = sGpsTimestampPattern.matcher(value);
if (!m.find()) {
Log.w(TAG, "Invalid value for " + tag + " : " + value);
return;
}
value = Integer.parseInt(m.group(1)) + "/1," + Integer.parseInt(m.group(2)) + "/1,"
+ Integer.parseInt(m.group(3)) + "/1";
} else {
try {
double doubleValue = Double.parseDouble(value);
value = (long) (doubleValue * 10000L) + "/10000"; // 精度丢失原因
} catch (NumberFormatException e) {
Log.w(TAG, "Invalid value for " + tag + " : " + value);
return;
}
}
}
。。。
}
因为这里有一个类型强转:value = (long) (doubleValue * 10000L) + "/10000";
当我的 doubleValue 为 0.00025,乘以 10000 就是 2.5,但是 long 强转之后就变成了 2 ,导致最后 value = 1/5000,与一开始的1/4000不一致了。
有什么其他接口可以让我写入曝光时间 (TAG_EXPOSURE_TIME)时不丢失精度吗?