Index: trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java	(revision 6126)
+++ trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java	(revision 6127)
@@ -65,9 +65,10 @@
 import com.drew.imaging.jpeg.JpegMetadataReader;
 import com.drew.lang.CompoundException;
+import com.drew.lang.GeoLocation;
 import com.drew.lang.Rational;
 import com.drew.metadata.Directory;
 import com.drew.metadata.Metadata;
 import com.drew.metadata.MetadataException;
-import com.drew.metadata.exif.ExifDirectory;
+import com.drew.metadata.exif.ExifIFD0Directory;
 import com.drew.metadata.exif.GpsDirectory;
 
@@ -508,13 +509,11 @@
     private static void extractExif(ImageEntry e) {
 
-        double deg;
-        double min, sec;
-        double lon, lat;
-        Metadata metadata = null;
-        Directory dirExif = null, dirGps = null;
+        Metadata metadata;
+        Directory dirExif;
+        GpsDirectory dirGps;
 
         try {
             metadata = JpegMetadataReader.readMetadata(e.getFile());
-            dirExif = metadata.getDirectory(ExifDirectory.class);
+            dirExif = metadata.getDirectory(ExifIFD0Directory.class);
             dirGps = metadata.getDirectory(GpsDirectory.class);
         } catch (CompoundException p) {
@@ -522,10 +521,20 @@
             e.setPos(null);
             return;
+        } catch (IOException p) {
+            e.setExifCoor(null);
+            e.setPos(null);
+            return;
         }
 
         try {
-            int orientation = dirExif.getInt(ExifDirectory.TAG_ORIENTATION);
+            int orientation = dirExif.getInt(ExifIFD0Directory.TAG_ORIENTATION);
             e.setExifOrientation(orientation);
         } catch (MetadataException ex) {
+        }
+
+        if (dirGps == null) {
+            e.setExifCoor(null);
+            e.setPos(null);
+            return;
         }
 
@@ -541,61 +550,22 @@
 
         try {
-            // longitude
-
-            Rational[] components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE);
-
-            deg = components[0].doubleValue();
-            min = components[1].doubleValue();
-            sec = components[2].doubleValue();
-
-            if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
-                throw new IllegalArgumentException();
-
-            lon = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
-
-            if (dirGps.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF).charAt(0) == 'W') {
-                lon = -lon;
-            }
-
-            // latitude
-
-            components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE);
-
-            deg = components[0].doubleValue();
-            min = components[1].doubleValue();
-            sec = components[2].doubleValue();
-
-            if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
-                throw new IllegalArgumentException();
-
-            lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
-
-            if (Double.isNaN(lat))
-                throw new IllegalArgumentException();
-
-            if (dirGps.getString(GpsDirectory.TAG_GPS_LATITUDE_REF).charAt(0) == 'S') {
-                lat = -lat;
-            }
-
-            // Store values
-
-            e.setExifCoor(new LatLon(lat, lon));
-            e.setPos(e.getExifCoor());
-
-        } catch (CompoundException p) {
-            // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220)
-            try {
-                Double longitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE);
-                Double latitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE);
-                if (longitude == null || latitude == null)
-                    throw new CompoundException("");
-
-                // Store values
-
-                e.setExifCoor(new LatLon(latitude, longitude));
+            GeoLocation location = dirGps.getGeoLocation();
+            if (location != null) {
+                e.setExifCoor(new LatLon(location.getLatitude(), location.getLongitude()));
                 e.setPos(e.getExifCoor());
-            } catch (CompoundException ex) {
-                e.setExifCoor(null);
-                e.setPos(null);
+            } else {
+                // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220)
+                try {
+                    Double longitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE);
+                    Double latitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE);
+    
+                    // Store values
+    
+                    e.setExifCoor(new LatLon(latitude, longitude));
+                    e.setPos(e.getExifCoor());
+                } catch (CompoundException ex) {
+                    e.setExifCoor(null);
+                    e.setPos(null);
+                }
             }
         } catch (Exception ex) { // (other exceptions, e.g. #5271)
Index: trunk/src/org/openstreetmap/josm/tools/ExifReader.java
===================================================================
--- trunk/src/org/openstreetmap/josm/tools/ExifReader.java	(revision 6126)
+++ trunk/src/org/openstreetmap/josm/tools/ExifReader.java	(revision 6127)
@@ -3,7 +3,7 @@
 
 import java.io.File;
+import java.io.IOException;
 import java.text.ParseException;
 import java.util.Date;
-import java.util.Iterator;
 
 import com.drew.imaging.jpeg.JpegMetadataReader;
@@ -13,5 +13,6 @@
 import com.drew.metadata.MetadataException;
 import com.drew.metadata.Tag;
-import com.drew.metadata.exif.ExifDirectory;
+import com.drew.metadata.exif.ExifIFD0Directory;
+import com.drew.metadata.exif.ExifSubIFDDirectory;
 
 /**
@@ -26,13 +27,12 @@
             String dateStr = null;
             OUTER:
-            for (Iterator<Directory> dirIt = metadata.getDirectoryIterator(); dirIt.hasNext();) {
-                for (Iterator<Tag> tagIt = dirIt.next().getTagIterator(); tagIt.hasNext();) {
-                    Tag tag = tagIt.next();
-                    if (tag.getTagType() == ExifDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) {
+            for (Directory dirIt : metadata.getDirectories()) {
+                for (Tag tag : dirIt.getTags()) {
+                    if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) {
                         dateStr = tag.getDescription();
                         break OUTER; // prefer this tag
                     }
-                    if (tag.getTagType() == ExifDirectory.TAG_DATETIME /* 0x0132 */ ||
-                        tag.getTagType() == ExifDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
+                    if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */ ||
+                        tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
                         dateStr = tag.getDescription();
                     }
@@ -55,9 +55,11 @@
         try {
             final Metadata metadata = JpegMetadataReader.readMetadata(filename);
-            final Directory dir = metadata.getDirectory(ExifDirectory.class);
-            orientation = dir.getInt(ExifDirectory.TAG_ORIENTATION);
+            final Directory dir = metadata.getDirectory(ExifIFD0Directory.class);
+            orientation = dir.getInt(ExifIFD0Directory.TAG_ORIENTATION);
         } catch (JpegProcessingException e) {
             e.printStackTrace();
         } catch (MetadataException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
             e.printStackTrace();
         }
