Den*_*erg 2 android exif androidx android-10.0 android-exifinterface
我的问题是 Android 10 无法从我的 jpeg 文件中读取 GPS 信息。完全相同的文件在 Android 9 上工作。我尝试了返回 null 的 ExifInterface (androidx),以及 apache commons 成像,它说“GPSLatitude:无效的理性(0/0),无效的理性(0/0)”。其他 exif 信息如评级或 XPKeywords 被正确读取。
如何解决?
测试代码:
import androidx.exifinterface.media.ExifInterface;
ExifInterface exif2 = new ExifInterface(is);
double[] latLngArray = exif2.getLatLong();
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
Android Q/10 上的所有 getAttribute 调用都返回 null。在 Android 9 上它正在工作。我的测试照片确实包含 GPS 信息。甚至 Android 本身也无法将 GPS 位置索引到他们的媒体商店中。该字段在他们的数据库中也为空。我通过邮件传输了测试照片。
正如开发者 android 中提到的,现在,在 Android 10 上,嵌入在图像文件中的位置不能直接用于应用程序:
但是,由于此位置信息很敏感,因此默认情况下,Android 10 会在您的应用使用范围存储时隐藏此信息。
在同一个链接上,您可以检查如何修复:
ACCESS_MEDIA_LOCATION在您的应用清单中请求权限。
从您的 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];
}
Run Code Online (Sandbox Code Playgroud)
请注意他们现在如何使用Uri, InputStream(以及FileDescriptor)来访问文件,因为现在您无法使用文件路径访问文件。
| 归档时间: |
|
| 查看次数: |
2126 次 |
| 最近记录: |