diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/FilePathConverter.java b/commons/src/main/java/fr/free/nrw/commons/upload/FilePathConverter.java index f8fff7a8a..8d1711f47 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/FilePathConverter.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/FilePathConverter.java @@ -23,27 +23,36 @@ public class FilePathConverter { * May return null */ public String getFilePath(){ + String filePath =""; - // Will return "image:x*" - String wholeID = DocumentsContract.getDocumentId(uri); - // Split at colon, use second item in the array - String id = wholeID.split(":")[1]; - String[] column = { MediaStore.Images.Media.DATA }; + try { + // Will return "image:x*" + String wholeID = DocumentsContract.getDocumentId(uri); - // where id is equal to - String sel = MediaStore.Images.Media._ID + "=?"; - Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - column, sel, new String[]{id}, null); + // Split at colon, use second item in the array + String id = wholeID.split(":")[1]; + String[] column = {MediaStore.Images.Media.DATA}; - int columnIndex = cursor.getColumnIndex(column[0]); + // where id is equal to + String sel = MediaStore.Images.Media._ID + "=?"; + Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + column, sel, new String[]{id}, null); - if (cursor.moveToFirst()) { - filePath = cursor.getString(columnIndex); + int columnIndex = cursor.getColumnIndex(column[0]); + + if (cursor.moveToFirst()) { + filePath = cursor.getString(columnIndex); + } + cursor.close(); + + Log.d("Image", "File path: " + filePath); + return filePath; } - cursor.close(); - Log.d("Image", "File path: " + filePath); - return filePath; + catch (IllegalArgumentException e) { + Log.w("Image", e); + return null; + } } } diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index d8c6b62e7..a72ae4f4e 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -26,6 +26,17 @@ public class GPSExtractor { try { exif = new ExifInterface(filePath); + + } catch (IOException e) { + Log.w("Image", e); + return null; + } + + if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) { + Log.w("Image", "Picture has no GPS info"); + return null; + } + else { latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE); latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); @@ -35,11 +46,8 @@ public class GPSExtractor { Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); - - } catch (IOException e) { - Log.w("Image", e); + return decimalCoords; } - return decimalCoords; } //Converts format of coords into decimal coords as required by API for next step diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java b/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java index f51c4703c..8186481c2 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java @@ -179,19 +179,23 @@ public class ShareActivity FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); - //extract the coordinates of image in decimal degrees - GPSExtractor imageObj = new GPSExtractor(filePath); - String coords = imageObj.getCoords(); - Log.d("Image", "Coords of image: " + coords); + if (filePath != null) { + //extract the coordinates of image in decimal degrees + GPSExtractor imageObj = new GPSExtractor(filePath); + String coords = imageObj.getCoords(); - MwVolleyApi apiCall = new MwVolleyApi(this); + if (coords != null) { + Log.d("Image", "Coords of image: " + coords); + MwVolleyApi apiCall = new MwVolleyApi(this); - //build URL with image coords for MediaWiki API calls - String apiUrl = apiCall.buildUrl(coords); - Log.d("Image", "URL: " + apiUrl); + //build URL with image coords for MediaWiki API calls + String apiUrl = apiCall.buildUrl(coords); + Log.d("Image", "URL: " + apiUrl); - //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories - apiCall.request(apiUrl); + //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories + apiCall.request(apiUrl); + } + } ImageLoader.getInstance().displayImage(mediaUriString, backgroundImageView);