Search code examples
androidimageimage-processinglocationjpeg

Save and Retrieve location inside JPEG image


Am going to share a solution which include save and retrieve location inside a JPEG image file.The latitude and longitude is save and retrive inside the image metadata using ExifInterface More about ExifInterface can be found here http://developer.android.com/reference/android/media/ExifInterface.html


Solution

  • public void saveLocation() {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitude);
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitude);
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void retriveLocation() {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
            String[] latitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE).split(",");
            String[] longitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE).split(",");
            String[] tmp = new String[2];
            tmp = latitudeValue[0].split("/");
            setLatitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
            tmp = longitudeValue[0].split("/");
            setLongitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    
    }