[英]Android Exif data not stored in picture file
我正在尝试在我的应用程序内生成的图像上插入一些位置数据(即它不是从设备相机拍摄的照片):
这是saveImage
方法:
public static String saveImage(Context context, ContentResolver contentResolver, Bitmap source,
String title, String description, Location location) {
File snapshot;
Uri url;
try {
File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File rpi = new File(pictures, context.getString(R.string.app_name));
if (!rpi.exists())
if (!rpi.mkdirs())
return null;
snapshot = new File(rpi, title);
OutputStream stream = new FileOutputStream(snapshot);
source.compress(Bitmap.CompressFormat.JPEG, 90, stream);
stream.flush();
stream.close();
if (location != null)
georeferenceImage(snapshot, location);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, title);
values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
if (description != null) {
values.put(MediaStore.Images.Media.DESCRIPTION, description);
}
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.ImageColumns.BUCKET_ID, snapshot.toString().toLowerCase(Locale.US).hashCode());
values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, snapshot.getName().toLowerCase(Locale.getDefault()));
}
values.put("_data", snapshot.getAbsolutePath());
url = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (Exception ex) {
return null;
}
return (url != null) ? url.toString() : null;
}
这是georeferenceImage()
方法:
private static boolean georeferenceImage(@NonNull final File image_file, @NonNull final Location location) {
try {
final ExifInterface exif = new ExifInterface(image_file.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, getLat(location));
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, location.getLatitude() < 0 ? "S" : "N");
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, getLon(location));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, location.getLongitude() < 0 ? "W" : "E");
//exif.setLatLong(location.getLatitude(), location.getLongitude());
//exif.setAltitude(location.getAltitude());
exif.saveAttributes();
} catch (IOException e) {
return false;
}
return true;
}
这些是 lat & lon 格式化方法:
private static String getLon(@NonNull final Location location) {
String[] degMinSec = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS).split(":");
return degMinSec[0] + "/1," + degMinSec[1] + "/1," + degMinSec[2] + "/1000";
}
private static String getLat(@NonNull final Location location) {
String[] degMinSec = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS).split(":");
return degMinSec[0] + "/1," + degMinSec[1] + "/1," + degMinSec[2] + "/1000";
}
保存后,我看不到 position 数据(我尝试使用不同的工具得到相同的结果)。 我还尝试使用setLatLon()
和setAltitude()
方法,但没有运气。 不抛出异常。 检查调试exif
变量之前saveAttributes()
调用我发现只有TAG_GPS_LATITUDE_REF
和TAG_GPS_LONGITUDE_REF
而不是TAG_GPS_LATITUDE
和TAG_GPS_LONGITUDE
。
问题在于 getLat() 和 getLon() 格式化程序。 这段代码效果更好:
private static String toExifFmt(double angle) {
angle = Math.abs(angle);
final int degree = (int) angle;
angle *= 60;
angle -= (degree * 60.0d);
final int minute = (int) angle;
angle *= 60;
angle -= (minute * 60.0d);
final int second = (int) (angle*1000.0d);
final StringBuilder sb = new StringBuilder();
sb.setLength(0);
sb.append(degree);
sb.append("/1,");
sb.append(minute);
sb.append("/1,");
sb.append(second);
sb.append("/1000");
return sb.toString();
}
我在这里找到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.