可以使用下面的代码示例来读取和更新图片的EXIF日期:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.media.ExifInterface;
public static void updatePhotoDate(String imagePath) {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
} catch (IOException e) {
e.printStackTrace();
}
String date = "2021:01:01 00:00:00"; // new date you wish to set
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
if (exif != null) {
try {
// get current date
String currentDateTime = exif.getAttribute(ExifInterface.TAG_DATETIME);
Date d = sdf.parse(currentDateTime);
// set new date
Date d2 = sdf.parse(date);
String datetime = sdf.format(d2);
exif.setAttribute(ExifInterface.TAG_DATETIME, datetime);
exif.saveAttributes();
} catch (Exception e) {
e.printStackTrace();
}
}
}
此代码示例演示了如何读取ExifInterface对象并更新图片的EXIF日期。您可以将其添加到Android应用程序中以解决Android的照片和图库应用程序无法正确处理EXIF日期的问题。