简体   繁体   English

即使以肖像拍摄,三星机器人拍摄的图库图像也始终处于风景中

[英]Gallery images taken on Samsung androids always in landscape even if taken in portrait

Picking images from gallery with the following code: 使用以下代码从库中挑选图像:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, PICK_FILE_RESULT_CODE);

Getting result with the following code: 使用以下代码获得结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_FILE_RESULT_CODE) {
        if (resultCode == RESULT_OK && data != null && data.getData() != null) {
            // Get the Uri of the selected file
            Uri uri = data.getData();

            //Using Picasso to load uri to imageView
            //Image is in landscape even if it was taken in portrait

        }
    }
}

The code works fine for HTC and Nexus phones, but just for Samsung devices (Galaxy 5 and Galaxy 5 mini) the orientation is wrong if the photo was taken in portrait. 该代码适用于HTC和Nexus手机,但仅适用于三星设备(Galaxy 5和Galaxy 5 mini),如果照片是以肖像拍摄的,则方向错误。 When looking at the ExifInterface the orientation is undefined.. 在查看ExifInterface时,方向未定义。

File imageFile = new File(uri.getPath());
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
//orientation is always 0 for samsung devices = ORIENTATION_UNDEFINED

How can I present the images correctly alternative determine the correct orientation so that I can rotate the image? 如何正确显示图像另外确定正确的方向以便我可以旋转图像?

I was able to get the orientation with the following code: 我能够使用以下代码获得方向:

public static int getExifOrientation(Context context, Uri uri) {
    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = null;
    try {
        String id = DocumentsContract.getDocumentId(uri);
        id = id.split(":")[1];
        cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                CONTENT_ORIENTATION, MediaStore.Images.Media._ID + " = ?", new String[] { id }, null);
        if (cursor == null || !cursor.moveToFirst()) {
            return 0;
        }
        return cursor.getInt(0);
    } catch (RuntimeException ignored) {
        // If the orientation column doesn't exist, assume no rotation.
        return 0;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

https://vikaskanani.wordpress.com/2011/07/17/android-re-size-image-without-loosing-exif-information/ https://vikaskanani.wordpress.com/2011/07/17/android-re-size-image-without-loosing-exif-information/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM