美文网首页
Android 照片属性写入经纬度

Android 照片属性写入经纬度

作者: Lrxc | 来源:发表于2017-11-25 14:56 被阅读553次
image.png

拍照后保存到本地文件,借助ExifInterface
保存格式必须是JPEG,否则不支持写入编辑

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

写入方法很简单

try {
    //保存照片的经纬度信息
    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, ConvertUtils.convertToDegree(116.2353515625));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, ConvertUtils.convertToDegree(39.5379397452));
    exif.saveAttributes();
    //打印结果
    String latValue = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
    String lngValue = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
    Log.i(TAG, "onCameraData: " + ConvertUtils.convertToCoordinate(latValue) + "--" + ConvertUtils.convertToCoordinate(lngValue));
} catch (IOException e) {
    e.printStackTrace();
}

ConvertUtils 工具类

public class ConvertUtils {
    //经纬度转度分秒
    public static String convertToDegree(double gpsInfo) {
        String dms = Location.convert(gpsInfo, Location.FORMAT_SECONDS);
        return dms;
    }

    //度分秒转经纬度
    public static Double convertToCoordinate(String stringDMS) {
        if (stringDMS == null) return null;
        String[] split = stringDMS.split(":", 3);
        return Double.parseDouble(split[0]) + Double.parseDouble(split[1]) / 60 + Double.parseDouble(split[2]) / 3600;
    }
}

搞定。 最后别忘了文件读写权限

相关文章

网友评论

      本文标题:Android 照片属性写入经纬度

      本文链接:https://www.haomeiwen.com/subject/oylcbxtx.html