在 Android Q/10 上无法读取 ExifInterface GPS [英] ExifInterface GPS not read on Android Q/10

查看:82
本文介绍了在 Android Q/10 上无法读取 ExifInterface GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having the issue that Android 10 can't read GPS information from my jpeg files. The exact same files are working on Android 9. I tried the ExifInterface (androidx) which returns null, as well as apache commons imaging which is saying "GPSLatitude: Invalid rational (0/0), Invalid rational (0/0)". Other exif information like the rating or XPKeywords are read correctly.

如何解决这个问题?

测试代码:

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

getLatLong 实现:

getLatLong Implementation:

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;
}

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

All getAttribute calls are returning null on Android Q/10. On Android 9 it is working. My test photo does contain GPS information. Even Android itself was unable to index the GPS location into their media store. The field is null as well in their database. I transferred the test photo via mail.

推荐答案

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

As mentioned in developer android, now, on Android 10, the location embed on image files is not directly available to the apps:

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

Because this location information is sensitive, however, Android 10 by default hides this information from your app if it uses scoped storage.

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

On that same link, you can check how to fix:

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

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

From your MediaStore object, call setRequireOriginal(), passing in the URI of the photograph, as shown in the following code snippet:

代码片段:

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];
}

注意他们现在如何使用 UriInputStream(还有 FileDescriptor)来访问文件,因为现在你不能访问使用其文件路径的文件.

Note how they are now using Uri, InputStream (and also FileDescriptor) to access the file since now, you can't access a file using its file path.

这篇关于在 Android Q/10 上无法读取 ExifInterface GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆