Android:JPEG EXIF信息添加
16lz
2021-01-23
ExifInterface:https://developer.android.com/reference/android/media/ExifInterface
添加信息:
ExifInterface exif = new ExifInterface(imagePath); // 写入经度信息 exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,gpsInfoConvert(lng)); exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,lng > 0 ? "E" : "W"); exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,gpsInfoConvert(lat)); exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,lat > 0 ? "N" : "S"); exif.setAttribute(ExifInterface.TAG_DATETIME,getNowTime("YYYY:MM:DD HH:mm:SS")); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ //exif.setAttribute(ExifInterface.TAG_USER_COMMENT,ToPinyin(address)); exif.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL,getNowTime("YYYY:MM:DD HH:mm:SS")); } exif.saveAttributes();
说明:
1、此处的经纬度是度分秒格式,不同于地图api定位后拿到的经纬度。
2、DATETIME 格式固定的(官方没看到具体的格式)YYYY:MM:DD HH:mm:SS
3、TAG_DATETIME_ORIGINAL AndroidM之后才支持
4、Android官方TAG_USER_COMMENT 不支持UTF-8
private static final Charset ASCII = Charset.forName("US-ASCII");public static ExifAttribute createString(String value) { final byte[] ascii = (value + '\0').getBytes(ASCII); return new ExifAttribute(IFD_FORMAT_STRING, ascii.length, ascii);}//就是说用ExifInterface 写进去的string都会被ASCII编码,反过来,读取的时候可以重写getAttribute,读取string时重新编码。//可以读取汉字数据,写不进去(需要用第三方库写,如下)
怎么添加TAG_USER_COMMENT 中文?
JpegInfo inJpegInfo = new JpegInfo(file);try { inJpegInfo.set(ExifTagConstants.EXIF_TAG_USER_COMMENT, address); rewriteInplace(inJpegInfo, file);} catch (ImageWriteException e) { e.printStackTrace();} catch (ImageReadException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}//此处参考:https://www.jianshu.com/p/f3c01883cc7e
更多相关文章
- Android 时间格式转换
- android如何获取经纬度
- android设置Spinner字体颜色,格式,大小
- android实现json数据的解析和把数据转换成json格式的字符串
- Android中的Bmp图形格式(ALPHA_8,ARGB_4444,ARGB_8888,RGB_565)
- Android Logcat 直接输出JSON格式的数据