Skip to content

Instantly share code, notes, and snippets.

@dnkoutso
Last active September 20, 2020 01:45
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dnkoutso/9e34a1bba3d63034e863 to your computer and use it in GitHub Desktop.
Save dnkoutso/9e34a1bba3d63034e863 to your computer and use it in GitHub Desktop.
@TargetApi(Build.VERSION_CODES.KITKAT)
static class DocumentExifTransformation implements Transformation {
private static final String[] CONTENT_ORIENTATION = new String[] {
MediaStore.Images.ImageColumns.ORIENTATION
};
final Context context;
final Uri uri;
DocumentExifTransformation(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;
if (!DocumentsContract.isDocumentUri(context, uri)) 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(" + DocumentsContract.getDocumentId(uri) + ")";
}
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();
}
}
}
}
@NemSim
Copy link

NemSim commented Apr 6, 2016

Thanks for writing this class up! It's helping me a great deal so far.

Something to look out for, I had to update the key method to guard against non-document IDs:

@Override
public String key() {
        if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) || 
                !DocumentsContract.isDocumentUri(context, uri)) return "documentTransform()";
        return "documentExifTransform(" + DocumentsContract.getDocumentId(uri) + ")";
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment