在Android 7上设置图像上的Exif数据始终返回错误

时间:2018-03-18 19:20:14

标签: android latitude-longitude exif android-gps

我必须在我的Image上设置exif数据。在Android 8中我的代码工作正常,但在Android 7中它报告下面的错误,之后没有exif数据保存在图像上:

  

W / ExifInterface:给定标记(GPSLatitude)值与预期格式之一不匹配:URATIONAL(guess:STRING)

这是我的代码:

    public void geoTag(String filename, double lng, double lat){
    ExifInterface exif;

    try {
        exif = new ExifInterface(filename);
        int num1Lat = (int)Math.floor(lat);
        int num2Lat = (int)Math.floor((lat - num1Lat) * 60);
        double num3Lat = (lat - ((double)num1Lat+((double)num2Lat/60))) * 3600000;

        int num1Lon = (int)Math.floor(lng);
        int num2Lon = (int)Math.floor((lng - num1Lon) * 60);
        double num3Lon = (lng - ((double)num1Lon+((double)num2Lon/60))) * 3600000;

        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");
   ...........

1 个答案:

答案 0 :(得分:0)

@greenapps ....感谢你的时间。基本上我有我的代码从地图检索位置并保存在SharedPref:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="LatLng">11.812703;42.081890</string>
</map>

之后,我有一个方法可以在经度和纬度上分割该字符串:

    public final void notifyMediaStoreScanner(final File file,Activity mainActivityCatched) {
    double longituderetrieve;
    double latituderetrieve;
    try {
        MediaStore.Images.Media.insertImage(mainActivityCatched.getContentResolver(),
                file.getAbsolutePath(), file.getName(), null);
        mainActivityCatched.sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

  //  SharedPref getPosition = new SharedPref(mainActivityCatched);
    SharedPreferences getLatLng = mainActivityCatched.getSharedPreferences("settingModeLatLng",MODE_PRIVATE);
    String LatLngShared = getLatLng.getString("LatLng","currentLatLng");
    latituderetrieve = Double.parseDouble(LatLngShared.substring(0,LatLngShared.indexOf(";")));
    longituderetrieve = Double.parseDouble(LatLngShared.substring(LatLngShared.indexOf(";")+1));

    try {
        geoTag(file.getAbsolutePath(),longituderetrieve,latituderetrieve);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

...最后我将所有值传递给另一个方法来设置exif数据中的位置。我原来的代码是:

 public void geoTag(String filename, double longi, double lati) throws IOException {


         ExifInterface exif;
 try {
        exif = new ExifInterface(filename);
        int num1Lat = (int)Math.floor(lat);
        int num2Lat = (int)Math.floor((lat - num1Lat) * 60);
        double num3Lat = (lat - ((double)num1Lat+((double)num2Lat/60))) * 3600000;

        int num1Lon = (int)Math.floor(lng);
        int num2Lon = (int)Math.floor((lng - num1Lon) * 60);
        double num3Lon = (lng - ((double)num1Lon+((double)num2Lon/60))) * 3600000;

        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");


        if (lat > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
        }

        if (lng > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
        }

        exif.saveAttributes();
    } catch (IOException e) {
        Log.e("PictureActivity", e.getLocalizedMessage());
    }

我的代码奇怪的是Android api26的作品.... 任何建议都非常感谢! 亚历