Example usage for android.media ExifInterface getLatLong

List of usage examples for android.media ExifInterface getLatLong

Introduction

In this page you can find the example usage for android.media ExifInterface getLatLong.

Prototype

public boolean getLatLong(float output[]) 

Source Link

Document

Stores the latitude and longitude value in a float array.

Usage

From source file:Main.java

public static boolean hasGPSInfo(String path) {

    try {/*from   ww w .  j a va  2  s  . c o m*/
        ExifInterface exf = new ExifInterface(path);
        float[] latlnt = new float[2];
        exf.getLatLong(latlnt);
        if (latlnt[0] <= 0 || latlnt[1] <= 0) {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static float[] getGeoLocationFromPhoto(String photo_path) {
    ExifInterface exif;
    float latlong[] = { (float) 0.0, (float) 0.0 };
    try {//from w  w w . j a  v a2 s  . c  o  m
        exif = new ExifInterface(photo_path);
        exif.getLatLong(latlong);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return latlong;
}

From source file:com.google.android.apps.muzei.gallery.GalleryArtSource.java

private void ensureMetadataExists(@NonNull Uri imageUri) {
    Cursor existingMetadata = getContentResolver().query(GalleryContract.MetadataCache.CONTENT_URI,
            new String[] { BaseColumns._ID }, GalleryContract.MetadataCache.COLUMN_NAME_URI + "=?",
            new String[] { imageUri.toString() }, null);
    if (existingMetadata == null) {
        return;/*from  ww  w.  ja  va 2 s. co m*/
    }
    boolean metadataExists = existingMetadata.moveToFirst();
    existingMetadata.close();
    if (!metadataExists) {
        // No cached metadata or it's stale, need to pull it separately using Exif
        ContentValues values = new ContentValues();
        values.put(GalleryContract.MetadataCache.COLUMN_NAME_URI, imageUri.toString());

        InputStream in = null;
        try {
            ExifInterface exifInterface;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                in = getContentResolver().openInputStream(imageUri);
                exifInterface = new ExifInterface(in);
            } else {
                File imageFile = GalleryProvider.getLocalFileForUri(this, imageUri);
                if (imageFile == null) {
                    return;
                }
                exifInterface = new ExifInterface(imageFile.getPath());
            }
            String dateString = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
            if (!TextUtils.isEmpty(dateString)) {
                Date date = sExifDateFormat.parse(dateString);
                values.put(GalleryContract.MetadataCache.COLUMN_NAME_DATETIME, date.getTime());
            }

            float[] latlong = new float[2];
            if (exifInterface.getLatLong(latlong)) {
                // Reverse geocode
                List<Address> addresses = mGeocoder.getFromLocation(latlong[0], latlong[1], 1);
                if (addresses != null && addresses.size() > 0) {
                    Address addr = addresses.get(0);
                    String locality = addr.getLocality();
                    String adminArea = addr.getAdminArea();
                    String countryCode = addr.getCountryCode();
                    StringBuilder sb = new StringBuilder();
                    if (!TextUtils.isEmpty(locality)) {
                        sb.append(locality);
                    }
                    if (!TextUtils.isEmpty(adminArea)) {
                        if (sb.length() > 0) {
                            sb.append(", ");
                        }
                        sb.append(adminArea);
                    }
                    if (!TextUtils.isEmpty(countryCode) && !sOmitCountryCodes.contains(countryCode)) {
                        if (sb.length() > 0) {
                            sb.append(", ");
                        }
                        sb.append(countryCode);
                    }
                    values.put(GalleryContract.MetadataCache.COLUMN_NAME_LOCATION, sb.toString());
                }
            }

            getContentResolver().insert(GalleryContract.MetadataCache.CONTENT_URI, values);
        } catch (ParseException e) {
            Log.w(TAG, "Couldn't read image metadata.", e);
        } catch (IOException e) {
            Log.w(TAG, "Couldn't write temporary image file.", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            }
        }
    }
}

From source file:org.jraf.android.piclabel.app.form.FormActivity.java

protected ImageInfo extractImageInfo(File file) {
    ImageInfo res = new ImageInfo();
    ExifInterface exifInterface = null;
    try {//from   w w w.ja v  a2  s  .c  o  m
        exifInterface = new ExifInterface(file.getPath());
    } catch (IOException e) {
        Log.e(TAG, "extractImageInfo Could not read exif", e);
    }

    // Date
    String dateTimeStr = null;
    if (exifInterface != null)
        dateTimeStr = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
    if (TextUtils.isEmpty(dateTimeStr)) {
        // No date in exif: use 'local' date
        res.dateTime = DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
        res.isLocalDateTime = true;
    } else {
        res.dateTime = parseExifDateTime(dateTimeStr);
        if (res.dateTime == null) {
            // Date in exif could not be parsed: use 'local' date
            DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE
                    | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
            res.isLocalDateTime = true;
        }
    }

    // Location
    float[] latLon = new float[2];
    boolean latLonPresent = exifInterface != null && exifInterface.getLatLong(latLon);
    if (!latLonPresent) {
        // No location in exif: use 'local' location
        res.isLocalLocation = true;
        latLonPresent = getLatestLocalLocation(latLon);
        if (latLonPresent)
            res.location = reverseGeocode(latLon[0], latLon[1]);
    } else {
        res.location = reverseGeocode(latLon[0], latLon[1]);
    }
    if (res.location == null) {
        res.reverseGeocodeProblem = true;
        res.location = "";
    }
    return res;
}