Fixes crashes

Implements null checking to prevent app from crashing when URI is
invalid or when picture has no GPS information
This commit is contained in:
misaochan 2015-12-23 22:15:50 +13:00
parent 77b8ab0c4c
commit 4a400df5dd
3 changed files with 50 additions and 29 deletions

View file

@ -23,7 +23,10 @@ public class FilePathConverter {
* May return null
*/
public String getFilePath(){
String filePath ="";
try {
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uri);
@ -46,4 +49,10 @@ public class FilePathConverter {
Log.d("Image", "File path: " + filePath);
return filePath;
}
catch (IllegalArgumentException e) {
Log.w("Image", e);
return null;
}
}
}

View file

@ -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,12 +46,9 @@ 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;
}
}
//Converts format of coords into decimal coords as required by API for next step
private String getDecimalCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) {

View file

@ -179,11 +179,13 @@ public class ShareActivity
FilePathConverter uriObj = new FilePathConverter(this, mediaUri);
String filePath = uriObj.getFilePath();
if (filePath != null) {
//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 (coords != null) {
Log.d("Image", "Coords of image: " + coords);
MwVolleyApi apiCall = new MwVolleyApi(this);
//build URL with image coords for MediaWiki API calls
@ -192,6 +194,8 @@ public class ShareActivity
//asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories
apiCall.request(apiUrl);
}
}
ImageLoader.getInstance().displayImage(mediaUriString, backgroundImageView);