the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > ExifInterface is provided by the Android API, Its setAttribute method internally converts floating-point values to finite decimals represented by fractions, so it can suffer from precision loss. To avoid precision loss, you can use the third-party library Metadata-extractor for reading and writing Exif information, which supports handling more Exif data types, such as floating-point values. Here is a sample code for writing Exif information using the Metadata-extractor library:
// 导入库
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.ExifSubIFDDescriptor;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifIFD0Descriptor;
// 读取Exif信息
File imageFile = new File("path/to/image");
Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
// 获取Exif子标签信息
ExifSubIFDDirectory subIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
ExifSubIFDDescriptor subIFDDescriptor = new ExifSubIFDDescriptor(subIFDDirectory);
// 写入曝光时间
subIFDDirectory.setObject(ExifSubIFDDirectory.TAG_EXPOSURE_TIME, new Rational(1, 4000));
// 保存Exif信息到文件
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(metadata.getByteArray());
fos.close();
The sample code reads the Exif information in an image file using the Metadata-extractor library, then uses the ExifSubIFDDirectory and ExifSubIFDDescriptor objects to get the sublabel information for the Exif information, Finally, Rational objects are used to write the exposure time into the Exif information of the image and save the Exif information to a file. If you need to write other types of Exif data, refer to the documentation for the Metadata-extractor library.