如何将ExifInterface与流或URI一起使用 [英] How to use ExifInterface with a stream or URI

查看:355
本文介绍了如何将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;
}

这篇关于如何将ExifInterface与流或URI一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆