Image orientation bug with Picasso

Short memo about an issue I just had some issue on Android with Picasso library when loading an image from a URI on KitKat.

The issue on GitHub seems to be this one:

This gist is a workaround to the issue, but it did not work for me out of the box.

This seems to work only with document:// kind of URLs because of DocumentsContract.getDocumentId(uri).

Finally, here is the code I used to get this to work properly.

package com.example.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.provider.MediaStore;

import com.squareup.picasso.Transformation;

public class ExifTransformation implements Transformation {
    private static final String[] CONTENT_ORIENTATION = new String[] {

    final Context context;
    final Uri uri;

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

    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();

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

        return source;

    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;

        return cursor.getInt(0);

When loading the image, the following then gave the wanted result:

    .transform(new ExifTransformation(this, uri))
comments powered by Disqus
© 2021 Daniel Perez   Creative Commons License