元数据提取器中的 NullPointerException
NullPointerException in metadata-extractor
我正在使用元数据提取器库,从 phone 中的照片中读取 exif 数据。
https://github.com/drewnoakes/metadata-extractor
我想从元数据中获取 GPS 和一些特定标签,用于 example:Latitude 和经度、焦距。我写的代码如下:
...
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.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.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.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.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());
如果您的图像不包含 GPS 数据,则返回的目录将为空。请务必使用空检查。使用肯定包含 GPS 数据的图像再次测试。
干杯伙计们,你们都很好。调试后,我看到我的一些照片没有GPS,而且是空的。
我post控制空值的代码如下(这也可以应用于其他特定标签)
// 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
}
}
就这些了,有什么问题或者更好的实现,欢迎留言。
我正在使用元数据提取器库,从 phone 中的照片中读取 exif 数据。 https://github.com/drewnoakes/metadata-extractor
我想从元数据中获取 GPS 和一些特定标签,用于 example:Latitude 和经度、焦距。我写的代码如下:
...
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.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.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.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.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());
如果您的图像不包含 GPS 数据,则返回的目录将为空。请务必使用空检查。使用肯定包含 GPS 数据的图像再次测试。
干杯伙计们,你们都很好。调试后,我看到我的一些照片没有GPS,而且是空的。 我post控制空值的代码如下(这也可以应用于其他特定标签)
// 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
}
}
就这些了,有什么问题或者更好的实现,欢迎留言。