2015-11-24 32 views

回答

2
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(); 
    } 


} 
0

使用ExifInterface你可以得到以下從媒體信息。

變量聲明。使用

String mediaDateTime,attrLATITUDE,attrLATITUDE_REF,attrLONGITUDE,attrLONGITUDE_REF,zip, city,state, country;; 
Double Latitude, Longitude; 
List<Address> addresses; 
Geocoder geocoder; 

用戶ExifInterface follwing方式。

ExifInterface exifInterfaceMedia = new ExifInterface(<Your Image Path>); 
    // This will give you data and time 
    mediaDateTime = exifInterfaceMedia.getAttribute(ExifInterface.TAG_DATETIME); 

    attrLATITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE); 
    attrLATITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); 
    attrLONGITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); 
    attrLONGITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); 

    if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) { 
      valid = true; 

      if (attrLATITUDE_REF.equals("N")) { 
        Latitude = convertToDegree(attrLATITUDE); 
      } else { 
        Latitude = 0 - convertToDegree(attrLATITUDE); 
      } 
      if (attrLONGITUDE_REF.equals("E")) { 
        Longitude = convertToDegree(attrLONGITUDE); 
      } else { 
        Longitude = 0 - convertToDegree(attrLONGITUDE); 
      } 

      try { 
        addresses = geocoder.getFromLocation(Latitude, Longitude, 1); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
      if (addresses != null && addresses.size() > 0) { 

        zip = addresses.get(0).getPostalCode(); 
        city = addresses.get(0).getLocality(); 
        state = addresses.get(0).getAdminArea(); 
        country = addresses.get(0).getCountryName(); 
        if (zip != null) { 
         title += zip + ","; 
        } 
        if (city != null) { 
         title += city + ","; 
        } 
        if (state != null) { 
         title += state + ","; 
        } 
        if (country != null) { 
         title += country; 
        } 

      } else { 
        title = "Unknown Location"; 
       } 
} 

private Double convertToDegree(String stringDMS) { 
     Double result = null; 
     String[] DMS = stringDMS.split(",", 3); 

     String[] stringD = DMS[0].split("/", 2); 
     Double D0 = new Double(stringD[0]); 
     Double D1 = new Double(stringD[1]); 
     Double FloatD = D0/D1; 

     String[] stringM = DMS[1].split("/", 2); 
     Double M0 = new Double(stringM[0]); 
     Double M1 = new Double(stringM[1]); 
     Double FloatM = M0/M1; 

     String[] stringS = DMS[2].split("/", 2); 
     Double S0 = new Double(stringS[0]); 
     Double S1 = new Double(stringS[1]); 
     Double FloatS = S0/S1; 

     result = new Double(FloatD + (FloatM/60) + (FloatS/3600)); 

     return result; 
}