我的目标是 Android 13 并使用新的照片选择器来检索图像。例如
val photoPicker = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickMultipleVisualMedia()
) { uris: List<Uri> -> handleUris(context, uris) ) }) }
photoPicker.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
fun handleUris(context: Context, uris: List<Uri>) {
for (uri in uris) {
context.contentResolver.openInputStream(uri)!!.use { stream ->
ExifInterface(stream).let { exif ->
val dateTime = exif.dateTime // present
val latLng = exif.latLong // always null
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现了什么:
自 Android 10 起,当您检索媒体时,GPS 信息不再包含在 EXIF 数据中。根据 2019 年的这段视频,您需要请求运行时权限ACCESS_MEDIA_LOCATION。
他们解释说这是一个运行时权限,但用户无法在系统设置中看到该权限(这本身就很奇怪)。他们还声称您需要READ_EXTERNAL_STORAGE媒体许可。
这篇文档还讨论了媒体位置权限。
使用以下方式请求许可 …
android runtime-permissions android-jetpack-compose android-exifinterface photo-picker
您好,我有一个compileSdkVersion 30和targetSdkVersion 30的应用程序。由于我需要知道图像的方向,所以我写了这些:
val exif = ExifInterface(imageFile.absolutePath)
val orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL
)
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> rotate = 270
ExifInterface.ORIENTATION_ROTATE_180 -> rotate = 180
ExifInterface.ORIENTATION_ROTATE_90 -> rotate = 90
}
Run Code Online (Sandbox Code Playgroud)
但有一个例外显示如下:
java.io,FileNotFoundException:/storage/emulated/0/DCIM/Camera/xxx.jpg: open failed EACCESS(Permission denied)
...
at android.media.ExifInterface.<init>(ExifInterface.java.1389)
Run Code Online (Sandbox Code Playgroud)
我想做的是获取图像并了解其方向,但我在互联网上找不到任何示例。有人能给我提示吗?谢谢!
android filenotfoundexception permission-denied android-10.0 android-exifinterface
下面是我的代码-
try {
InputStream inputStream = getAssets().open("thumbnail.jpg");
exifInterface = new ExifInterface(inputStream);
exifInterface.setAttribute(ExifInterface.TAG_ARTIST,"TEST INPUT");
exifInterface.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我在线上exifInterface.saveAttributes()收到以下错误 -
java.io.IOException:ExifInterface 不支持保存当前输入的属性。
我不确定错误是由于图像文件还是由于属性引起的。我正在努力挽救。我还在网上寻找可能的解决方案(例如 Sanselan),但不确定它是否能解决这个问题。
有人可以解释如何解决这个问题吗?
谢谢!
我的问题是 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, …Run Code Online (Sandbox Code Playgroud) saveAttributes()当尝试使用 androidxExifinterface保存任何 JPEG 图片时,我的程序崩溃并出现错误“写入失败:EBADF(错误文件描述符)”
我可以从新项目重新开始复制错误。我正在使用 Android Studio:new project-> Empty Activity。我正在使用模拟器来测试。
下面是完整的代码,其中包含我对新的空活动模板所做的唯一更改。
\n使用androidxExifinterface,此代码能够正确获取 Exif 属性。然而,saveAttributes()每次都会崩溃:
saveAttributes()无论我是否先崩溃setAttribute()都会崩溃。saveAttributes()抛出:“无法保存新文件。原始文件存储在...”我想设置图片的Exif属性并保存到原始图像文件中。正确的方法是什么?
\n[这篇文章应该被标记androidx-interface。但该标签不存在,而且我没有添加标签的声誉。所以我使用了 tag android-interface,它确实存在]。
构建.gradle(:应用程序)
\nimplementation "androidx.exifinterface:exifinterface:1.3.2"\nRun Code Online (Sandbox Code Playgroud)\nAndroidManifest.xml:
\n <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />\n <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />\n <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />\nRun Code Online (Sandbox Code Playgroud)\nMainActivity.kt:
\n package com.example.test_exif_save\n \n import android.content.ContentUris\n import …Run Code Online (Sandbox Code Playgroud)