mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
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:
parent
77b8ab0c4c
commit
4a400df5dd
3 changed files with 50 additions and 29 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue