如何将ExifInterface与流或URI一起使用
我正在编写一个可以从Android中共享方式"菜单中发送照片URI的应用程序.
I'm writing an app that can be sent a photo URI from the "Share via" menu in Android.
您获得的URI的类型为content://media/external/images/media/556
,但是ExifInterface
需要一个标准文件名.那么,如何读取该文件的exif数据(我只想要方向)?这是我的(无效)代码:
The kind of URI you get is content://media/external/images/media/556
however ExifInterface
wants a standard file name. So how do I read the exif data (I just want orientation) of that file? Here's my (non-working) code:
Uri uri = (Uri)extras.getParcelable(Intent.EXTRA_STREAM);
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
Bitmap bm = BitmapFactory.decodeStream(is);
// This line doesn't work:
ExifInterface exif = new ExifInterface(uri.toString());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
任何帮助(除了您必须编写自己的ExifInterface类"之外)都很感激!
Any help (other than "you have to write your own ExifInterface class") is appreciated!
我在Facebook Android SDK示例中随机找到了答案.我没有测试过,但是看起来应该可以.这是代码:
I found the answer randomly in the Facebook Android SDK examples. I haven't tested it, but it looks like it should work. Here's the code:
public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
int result = -1;
if (null != cursor) {
if (cursor.moveToFirst()) {
result = cursor.getInt(0);
}
cursor.close();
}
return result;
}