【问题标题】:picasso image rotation issue in android 10 using Uri of imageandroid 10中使用图像Uri的毕加索图像旋转问题
【发布时间】:2021-07-26 04:01:18
【问题描述】:

Picasso 在 android 10 中使用图像的 Uri 显示时存在图像旋转问题

Uri uri = "content://media/external/images/media/12155";
Picasso.get().load(uri).into(imageView);

我用的是最新的 2.8 版本

这是原图

如果 android API 28 例如问题发生在大于或等于 android 29 时,则找不到此问题

【问题讨论】:

  • 有什么问题?像,它会自动旋转图像?你确定原版不是这样的吗?有时我们将手机侧向拍摄人像模式,并认为我们正在拍摄风景照片。
  • 这是原始照片i.ibb.co/QJjHBY1/IMG-20210503-090344.jpg,当我在 android 28 中显示图像时,它以原始方向工作
  • 如果您在模拟器中使用它,横向/纵向设置是否与您的 Android 8 设备相同?问是因为这是我唯一能想到的。我从来没有听说过像这样依赖毕加索的事情
  • 在 android 29 中尝试此图像 drive.google.com/file/d/1_VFpwrp3o6ZVUe7YmHcIDtF1PYI19NFz/… 并从清单中删除此标志 requestLegacyExternalStorage=true
  • @Fred 另请注意,当我使用 Glide 库显示相同的图像时,它的显示没有问题

标签: android picasso android-10.0


【解决方案1】:

这个类解决问题

https://daniel.perez.sh/blog/2015/image-orientation-picasso/

package com.example.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import com.squareup.picasso.Transformation;

@TargetApi(Build.VERSION_CODES.KITKAT)
public class ExifTransformation implements Transformation {
    private static final String[] CONTENT_ORIENTATION = new String[] {
            MediaStore.Images.ImageColumns.ORIENTATION
    };

    final Context context;
    final Uri uri;

    public ExifTransformation(Context context, Uri uri) {
        this.context = context;
        this.uri = uri;
    }

    @Override
    public Bitmap transform(Bitmap source) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return source;

        int exifRotation = getExifOrientation(context, uri);
        if (exifRotation != 0) {
            Matrix matrix = new Matrix();
            matrix.preRotate(exifRotation);

            Bitmap rotated =
                    Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
            if (rotated != source) {
                source.recycle();
            }
            return rotated;
        }

        return source;
    }

    @Override
    public String key() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return "documentTransform()";
        return "documentExifTransform(" + uri.toString() + ")";
    }

    public static int getExifOrientation(Context context, Uri photoUri) {
        Cursor cursor = context.getContentResolver().query(photoUri, CONTENT_ORIENTATION, null, null, null);

        if (cursor.getCount() != 1) {
            return -1;
        }

        cursor.moveToFirst();
        return cursor.getInt(0);
    }
}

加载图像时,以下内容给出了想要的结果:

Picasso
    .with(this)
    .load(uri)
    .transform(new ExifTransformation(this, uri))
    .into(imageView);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多