feat(android): Manually obtain location metadata using ExifInterface (#114)

* Manually obtain location metadata using ExifInterface

* import ExifInterface

* remove log statement

* removed unused code and put comments

* change error message text

* code formatting

Co-authored-by: Syed Rahaman <syed.rahaman@kartbites.com>
This commit is contained in:
Syed Rahaman 2020-04-30 17:44:37 +05:30 committed by GitHub
parent 53e42df323
commit ade785e29d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 13 deletions

View File

@ -21,6 +21,7 @@ import android.os.Environment;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.provider.MediaStore.Images; import android.provider.MediaStore.Images;
import android.text.TextUtils; import android.text.TextUtils;
import android.media.ExifInterface;
import com.facebook.common.logging.FLog; import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.GuardedAsyncTask; import com.facebook.react.bridge.GuardedAsyncTask;
@ -457,8 +458,6 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
int dateTakenIndex = media.getColumnIndex(Images.Media.DATE_TAKEN); int dateTakenIndex = media.getColumnIndex(Images.Media.DATE_TAKEN);
int widthIndex = media.getColumnIndex(MediaStore.MediaColumns.WIDTH); int widthIndex = media.getColumnIndex(MediaStore.MediaColumns.WIDTH);
int heightIndex = media.getColumnIndex(MediaStore.MediaColumns.HEIGHT); int heightIndex = media.getColumnIndex(MediaStore.MediaColumns.HEIGHT);
int longitudeIndex = media.getColumnIndex(Images.Media.LONGITUDE);
int latitudeIndex = media.getColumnIndex(Images.Media.LATITUDE);
int dataIndex = media.getColumnIndex(MediaStore.MediaColumns.DATA); int dataIndex = media.getColumnIndex(MediaStore.MediaColumns.DATA);
for (int i = 0; i < limit && !media.isAfterLast(); i++) { for (int i = 0; i < limit && !media.isAfterLast(); i++) {
@ -468,7 +467,7 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
putImageInfo(resolver, media, node, idIndex, widthIndex, heightIndex, dataIndex, mimeTypeIndex); putImageInfo(resolver, media, node, idIndex, widthIndex, heightIndex, dataIndex, mimeTypeIndex);
if (imageInfoSuccess) { if (imageInfoSuccess) {
putBasicNodeInfo(media, node, mimeTypeIndex, groupNameIndex, dateTakenIndex); putBasicNodeInfo(media, node, mimeTypeIndex, groupNameIndex, dateTakenIndex);
putLocationInfo(media, node, longitudeIndex, latitudeIndex); putLocationInfo(media, node, dataIndex);
edge.putMap("node", node); edge.putMap("node", node);
edges.pushMap(edge); edges.pushMap(edge);
@ -577,16 +576,25 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
private static void putLocationInfo( private static void putLocationInfo(
Cursor media, Cursor media,
WritableMap node, WritableMap node,
int longitudeIndex, int dataIndex) {
int latitudeIndex) { try {
double longitude = media.getDouble(longitudeIndex); // location details are no longer indexed for privacy reasons using string Media.LATITUDE, Media.LONGITUDE
double latitude = media.getDouble(latitudeIndex); // we manually obtain location metadata using ExifInterface#getLatLong(float[]).
if (longitude > 0 || latitude > 0) { // ExifInterface is added in API level 5
WritableMap location = new WritableNativeMap(); final ExifInterface exif = new ExifInterface(media.getString(dataIndex));
location.putDouble("longitude", longitude); float[] imageCoordinates = new float[2];
location.putDouble("latitude", latitude); boolean hasCoordinates = exif.getLatLong(imageCoordinates);
node.putMap("location", location); if (hasCoordinates) {
} double longitude = imageCoordinates[1];
double latitude = imageCoordinates[0];
WritableMap location = new WritableNativeMap();
location.putDouble("longitude", longitude);
location.putDouble("latitude", latitude);
node.putMap("location", location);
}
} catch (IOException e) {
FLog.e(ReactConstants.TAG, "Could not read the metadata", e);
}
} }
/** /**