-1

我正在使用元数据提取器库,从手机中的照片中读取 exif 数据。 https://github.com/drewnoakes/metadata-extractor

我想从元数据中获取 GPS 和一些特定标签,例如:纬度和经度,焦距。我编写代码如下:

...
Metadata metadata = ImageMetadataReader.readMetadata(file);
String metaDataString = "" ;
GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);    
metaDataString += "Long: " + String.valueOf(gpsDirectory.getGeoLocation().getLongitude()) + "Lat: " + String.valueOf(gpsDirectory.getGeoLocation().getLatitude());

ExifSubIFDDirectory exifSubIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
metaDataString += "[focal length]" + exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH);

...

我收到了上面代码的 NullException。请帮我弄清楚。提前致谢。

** 添加日志 **

java.lang.RuntimeException: An error occured while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:299)
       at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
       at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
       at java.util.concurrent.FutureTask.run(FutureTask.java:137)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
       at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
       at com.testing.MetaData.LoadPhoto.doInBackground(LoadPhoto.java:113)
       at android.os.AsyncTask$2.call(AsyncTask.java:287)
       at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
       at java.util.concurrent.FutureTask.run(FutureTask.java:137)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
       at java.lang.Thread.run(Thread.java:856)

代码行 113 处的 NPE 错误是

metaDataString += "Long: " + String.valueOf(gpsDirectory.getGeoLocation().getLongitude()) + "Lat: " + String.valueOf(gpsDirectory.getGeoLocation().getLatitude());
4

2 回答 2

1

加油伙计们,你们没事。调试后,我看到我的一些照片没有 GPS,并且为空。我发布代码来控制空值如下(这也可以应用于其他特定标签)

// Check if metadata contains the specific Directory
if (metadata.containsDirectoryOfType(GpsDirectory.class)) {

      GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);

      //Check if Directory contains the specific Tag
      if(gpsDirectory.containsTag(GpsDirectory.TAG_LATITUDE)&& gpsDirectory.containsTag(GpsDirectory.TAG_LONGITUDE)) {

          metaDataString = "[Longtitude]: " + String.valueOf(gpsDirectory.getGeoLocation().getLongitude()) + ", " +
                                    "[Latitude]: " + String.valueOf(gpsDirectory.getGeoLocation().getLatitude()) + ", ";
      }
      else {
        //Show error or notification
      }
}

就是这样,任何关注或更好的实施,请发表评论。

于 2015-08-07T13:02:50.867 回答
0

如果您的图像不包含 GPS 数据,则返回的目录将为空。请务必使用空检查。使用绝对包含 GPS 数据的图像再次测试。

于 2015-08-07T11:33:31.210 回答