如果选择了设备的主存储器,我需要使用正确的方案而不是使用content://com.android.externalstorage.documents/tree/primary:
的DocumentFile或Uri获取文件。 要获得文件或图像的绝对路径,我需要一个与file:/// storage/emulated/0或storage/emulated/0但我找不到一个方法来获取正确的Uri用于构建文件以写入EXIF数据印象。将EXIF数据写入使用DocumentFile类保存的图像
我的情况是:
- 用户选择保存与
content://com.android.externalstorage.documents
onActivityResult返回URI()图像的路径。我将此路径保存到SharedPreferences中以便稍后使用treeUri.toString()
。 - 用户拍照和图像保存
DocumentFile.fromTreeUri(MainActivity.this, Uri.parse(uriString));
- 这其中,我失败了,获得该正确地指向图像文件,乌里与内容://不返回现有image.Correct URI应该
file:///storage/emulated/
,我可以将此Uri转换为使用的文件File filePath = new File(URI.create(saveDir.getUri().toString()));
如何获取使用Uri构建文件或文件所需的Uri,我是从SAF UI获得的?
编辑:ExifInterface Support Library是Android 7.1 +可以使用InputStream或FileDescriptor引入。
Uri uri; // the URI you've received from the other app
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
ExifInterface exifInterface = new ExifInterface(in);
// Now you can extract any Exif tag you want
// Assuming the image is a JPEG or supported raw format
} catch (IOException e) {
// Handle any errors
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
注: ExifInterface不会与远程InputStreams,如那些从HttpURLConnection的返回工作。强烈建议只在content://或file:// URIs中使用它们。
上面的代码片段显然是读取数据,因为它打开InputStream来读取数据。我需要能够将EXIF数据写入JPEG文件。