2017-02-21 58 views
2

我試圖獲取圖像的GPS位置,但我得到的是空/ 0,我試圖從不同的圖像獲取其他EXIF信息,但結果仍然爲空/ 0所有ExifInterface值返回0或空

public void MarkGeoTagImage(String imagePath) 
{ 
    try { 
     ExifInterface exif = new ExifInterface(imagePath); 
Toast.makeText(MainActivity.this, imagePath, Toast.LENGTH_LONG).show(); 
     Toast.makeText(MainActivity.this, exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE),Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

第一個烤麪包顯示圖像的絕對路徑,如/storage/sdcard0/etp_images/test.jpg,但第二個烤麪包只顯示0或空作爲結果。

+0

用你自己的GPS經度圖像試試你的代碼。 –

+0

我做了,它沒有工作。 –

回答

2

也許這不是一個解決方案,但我之前有一些問題ExifInterface。原始類別ExifInterface有不同的錯誤。嘗試使用此類的支持版本。在我的情況下,它解決了我的問題。 編譯此使用搖籃:com.android.support:exifinterface:25.1.0

+0

非常感謝你!你的方法確實有效!最後 –

+0

不客氣:) – Kostya

+0

我在xamarin android中工作。我已經安裝了「Exif支持庫」,但仍面臨使用此鏈接:https://www.nuget.org/packages/Xamarin.Android.Support.Exif/25.4.0.2,但我面臨的問題。 –

1

我有同樣的問題,所以我還添加了ExifInterface支持庫到我的項目有以下依賴性:

compile 'com.android.support:exifinterface:25.1.0' 

,也寫了下面的方法,以獲得圖像取向角度:

private int getImageAngle(Uri uri, String imagePath) { 
    int orientation; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     orientation = getOrientation(uri); 
    } else { 
     orientation = getOrientationLegacy(imagePath); 
    } 

    switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      return 90; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      return 180; 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      return 270; 
    } 

    return 0; 
} 

@TargetApi(Build.VERSION_CODES.N) 
private int getOrientation(Uri uri) { 
    InputStream in = null; 
    ExifInterface exif = null; 

    try { 
     in = getContentResolver().openInputStream(uri); 
     exif = new ExifInterface(in); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException ignored) { 
      } 
     } 
    } 

    return getExifAttributeInt(exif); 
} 

private int getOrientationLegacy(String imagePath) { 
    ExifInterface exif = null; 
    try { 
     exif = new ExifInterface(imagePath); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return getExifAttributeInt(exif); 
} 

private int getExifAttributeInt(ExifInterface exif) { 
    if (exif != null) { 
     return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
    } else { 
     return ExifInterface.ORIENTATION_NORMAL; 
    } 
}