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,27 +23,36 @@ public class FilePathConverter {
* May return null * May return null
*/ */
public String getFilePath(){ public String getFilePath(){
String filePath =""; String filePath ="";
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array try {
String id = wholeID.split(":")[1]; // Will return "image:x*"
String[] column = { MediaStore.Images.Media.DATA }; String wholeID = DocumentsContract.getDocumentId(uri);
// where id is equal to // Split at colon, use second item in the array
String sel = MediaStore.Images.Media._ID + "=?"; String id = wholeID.split(":")[1];
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String[] column = {MediaStore.Images.Media.DATA};
column, sel, new String[]{id}, null);
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()) { int columnIndex = cursor.getColumnIndex(column[0]);
filePath = cursor.getString(columnIndex);
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); catch (IllegalArgumentException e) {
return filePath; Log.w("Image", e);
return null;
}
} }
} }

View file

@ -26,6 +26,17 @@ public class GPSExtractor {
try { try {
exif = new ExifInterface(filePath); 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 = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
@ -35,11 +46,8 @@ public class GPSExtractor {
Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); Log.d("Image", "Longitude: " + longitude + " " + longitude_ref);
decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
return decimalCoords;
} catch (IOException e) {
Log.w("Image", e);
} }
return decimalCoords;
} }
//Converts format of coords into decimal coords as required by API for next step //Converts format of coords into decimal coords as required by API for next step

View file

@ -179,19 +179,23 @@ public class ShareActivity
FilePathConverter uriObj = new FilePathConverter(this, mediaUri); FilePathConverter uriObj = new FilePathConverter(this, mediaUri);
String filePath = uriObj.getFilePath(); String filePath = uriObj.getFilePath();
//extract the coordinates of image in decimal degrees if (filePath != null) {
GPSExtractor imageObj = new GPSExtractor(filePath); //extract the coordinates of image in decimal degrees
String coords = imageObj.getCoords(); GPSExtractor imageObj = new GPSExtractor(filePath);
Log.d("Image", "Coords of image: " + coords); 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 //build URL with image coords for MediaWiki API calls
String apiUrl = apiCall.buildUrl(coords); String apiUrl = apiCall.buildUrl(coords);
Log.d("Image", "URL: " + apiUrl); Log.d("Image", "URL: " + apiUrl);
//asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories
apiCall.request(apiUrl); apiCall.request(apiUrl);
}
}
ImageLoader.getInstance().displayImage(mediaUriString, backgroundImageView); ImageLoader.getInstance().displayImage(mediaUriString, backgroundImageView);