use of android.media.ExifInterface in project apps-android-commons by commons-app.
the class GPSExtractor method getCoords.
/**
* Extracts geolocation (either of image from EXIF data, or of user)
* @param useGPS set to true if location permissions allowed (by API 23), false if disallowed
* @return coordinates as string (needs to be passed as a String in API query)
*/
@Nullable
public String getCoords(boolean useGPS) {
ExifInterface exif;
String latitude = "";
String longitude = "";
String latitude_ref = "";
String longitude_ref = "";
String decimalCoords = "";
try {
exif = new ExifInterface(filePath);
} catch (IOException e) {
Timber.w(e);
return null;
} catch (IllegalArgumentException e) {
Timber.w(e);
return null;
}
//If image has no EXIF data and user has enabled GPS setting, get user's location
if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null && useGPS) {
registerLocationManager();
imageCoordsExists = false;
Timber.d("EXIF data has no location info");
//Check what user's preference is for automatic location detection
boolean gpsPrefEnabled = gpsPreferenceEnabled();
//Check that currentLatitude and currentLongitude have been explicitly set by MyLocationListener and do not default to (0.0,0.0)
if (gpsPrefEnabled && currentLatitude != null && currentLongitude != null) {
Timber.d("Current location values: Lat = %f Long = %f", currentLatitude, currentLongitude);
return String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
} else {
// No coords found
return null;
}
} else if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) {
return null;
} else {
//If image has EXIF data, extract image coords
imageCoordsExists = true;
Timber.d("EXIF data has location info");
latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if (latitude != null && latitude_ref != null && longitude != null && longitude_ref != null) {
Timber.d("Latitude: %s %s", latitude, latitude_ref);
Timber.d("Longitude: %s %s", longitude, longitude_ref);
decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
return decimalCoords;
} else {
return null;
}
}
}
use of android.media.ExifInterface in project android_frameworks_base by DirtyUnicorns.
the class ExifInterfaceTest method testSaveAttributes_withFileName.
private void testSaveAttributes_withFileName(File imageFile, ExpectedValue expectedValue) throws IOException {
String verboseTag = imageFile.getName();
ExifInterface exifInterface = new ExifInterface(imageFile.getAbsolutePath());
exifInterface.saveAttributes();
exifInterface = new ExifInterface(imageFile.getAbsolutePath());
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
// Test for modifying one attribute.
String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
exifInterface.saveAttributes();
exifInterface = new ExifInterface(imageFile.getAbsolutePath());
assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
// Restore the backup value.
exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
exifInterface.saveAttributes();
exifInterface = new ExifInterface(imageFile.getAbsolutePath());
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
}
use of android.media.ExifInterface in project android_frameworks_base by DirtyUnicorns.
the class ExifInterfaceTest method testSaveAttributes_withFileDescriptor.
private void testSaveAttributes_withFileDescriptor(File imageFile, ExpectedValue expectedValue) throws IOException {
String verboseTag = imageFile.getName();
FileDescriptor fd = null;
try {
fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDWR, 0600);
ExifInterface exifInterface = new ExifInterface(fd);
exifInterface.saveAttributes();
Os.lseek(fd, 0, OsConstants.SEEK_SET);
exifInterface = new ExifInterface(fd);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
// Test for modifying one attribute.
String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
exifInterface.saveAttributes();
Os.lseek(fd, 0, OsConstants.SEEK_SET);
exifInterface = new ExifInterface(fd);
assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
// Restore the backup value.
exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
exifInterface.saveAttributes();
Os.lseek(fd, 0, OsConstants.SEEK_SET);
exifInterface = new ExifInterface(fd);
compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
} catch (ErrnoException e) {
throw e.rethrowAsIOException();
} finally {
IoUtils.closeQuietly(fd);
}
}
use of android.media.ExifInterface in project android_frameworks_base by DirtyUnicorns.
the class DocumentsContract method openImageThumbnail.
/**
* Open the given image for thumbnail purposes, using any embedded EXIF
* thumbnail if available, and providing orientation hints from the parent
* image.
*
* @hide
*/
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
Bundle extras = null;
try {
final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
case ExifInterface.ORIENTATION_ROTATE_90:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 270);
break;
}
final long[] thumb = exif.getThumbnailRange();
if (thumb != null) {
return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
}
} catch (IOException e) {
}
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
use of android.media.ExifInterface in project android_frameworks_base by AOSPA.
the class DocumentsContract method openImageThumbnail.
/**
* Open the given image for thumbnail purposes, using any embedded EXIF
* thumbnail if available, and providing orientation hints from the parent
* image.
*
* @hide
*/
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
Bundle extras = null;
try {
final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
case ExifInterface.ORIENTATION_ROTATE_90:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
extras = new Bundle(1);
extras.putInt(EXTRA_ORIENTATION, 270);
break;
}
final long[] thumb = exif.getThumbnailRange();
if (thumb != null) {
return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
}
} catch (IOException e) {
}
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
Aggregations