2

我遇到了 Android 10 无法从我的 jpeg 文件中读取 GPS 信息的问题。完全相同的文件在 Android 9 上运行。我尝试了返回 null 的 ExifInterface (androidx),以及显示“GPSLatitude:无效的理性 (0/0),无效的理性 (0/0)”的 apache commons 成像。其他 exif 信息(如评级或 XPKeywords)被正确读取。

如何解决?

测试代码:

import androidx.exifinterface.media.ExifInterface;
ExifInterface exif2 = new ExifInterface(is);
double[] latLngArray = exif2.getLatLong();

getLatLong 实现:

public double[] getLatLong() {
    String latValue = getAttribute(TAG_GPS_LATITUDE);
    String latRef = getAttribute(TAG_GPS_LATITUDE_REF);
    String lngValue = getAttribute(TAG_GPS_LONGITUDE);
    String lngRef = getAttribute(TAG_GPS_LONGITUDE_REF);
    if (latValue != null && latRef != null && lngValue != null && lngRef != null) {
        try {
            double latitude = convertRationalLatLonToDouble(latValue, latRef);
            double longitude = convertRationalLatLonToDouble(lngValue, lngRef);
            return new double[] {latitude, longitude};
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "Latitude/longitude values are not parseable. " +
                    String.format("latValue=%s, latRef=%s, lngValue=%s, lngRef=%s",
                            latValue, latRef, lngValue, lngRef));
        }
    }
    return null;
}

所有 getAttribute 调用在 Android Q/10 上都返回 null。在 Android 9 上它正在运行。我的测试照片确实包含 GPS 信息。甚至 Android 本身也无法将 GPS 位置索引到他们的媒体存储中。该字段在他们的数据库中也是空的。我通过邮件传送了测试照片。

4

1 回答 1

9

正如开发者 android中提到的,现在,在 Android 10 上,嵌入图像文件的位置不能直接用于应用程序:

但是,由于此位置信息很敏感,因此默认情况下,Android 10 会在您的应用使用范围存储时隐藏此信息。

在同一个链接上,您可以检查如何修复:

  1. ACCESS_MEDIA_LOCATION在您的应用清单中请求权限。

  2. 从您的 MediaStore 对象调用setRequireOriginal(),并传入照片的 URI,如以下代码片段所示:

代码片段:

Uri photoUri = Uri.withAppendedPath(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        cursor.getString(idColumnIndex));

// Get location data from the ExifInterface class.
photoUri = MediaStore.setRequireOriginal(photoUri);
InputStream stream = getContentResolver().openInputStream(photoUri);
if (stream != null) {
    ExifInterface exifInterface = new ExifInterface(stream);
    // Don't reuse the stream associated with the instance of "ExifInterface".
    stream.close();
} else {
    // Failed to load the stream, so return the coordinates (0, 0).
    latLong = new double[2];
}

请注意他们现在如何使用Uri, InputStream(以及FileDescriptor)来访问文件,因为现在,您无法使用文件路径访问文件。

于 2019-09-17T19:58:16.530 回答