如何从InputStream而不是File获取Exif数据? [英] How to get Exif data from an InputStream rather than a File?
问题描述
我之所以这样问是因为文件选择器 Intent
的回调返回 Uri
。
The reason why I am asking this is because the callback of the file chooser Intent
returns an Uri
.
通过Intent打开文件选择器:
Open file chooser via Intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), CHOOSE_IMAGE_REQUEST);
回调:
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data == null) {
// Error
return;
}
Uri fileUri = data.getData();
InputStream in = getContentResolver().openInputStream(fileUri);
// How to determine image orientation through Exif data here?
}
}
一种方法是写 InputStream
到一个实际的文件
,但这对我来说似乎是一个糟糕的解决方法。
One way would be to write the InputStream
to an actual File
, but this seems like a bad workaround for me.
推荐答案
引入25.1.0支持库后,现在可以通过<$ c $从URI内容(content://或file://)读取exif数据C>的InputStream 。
After the 25.1.0 support library was introduced, now is possible to read exif data from URI contents (content:// or file://) through an InputStream
.
示例:
首先将此行添加到您的gradle文件中:
Example: First add this line to your gradle file:
compile'com.android.support:exifinterface:25.1.0'
compile 'com.android.support:exifinterface:25.1.0'
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支持库, ExifInterface 。
For more information check: Introducing the ExifInterface Support Library, ExifInterface.
这篇关于如何从InputStream而不是File获取Exif数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!