-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed EXIF orientation in MediaStoreRequestHandler #1459
Open
st1hy
wants to merge
4
commits into
square:master
Choose a base branch
from
st1hy:media-store-orientation-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9dee444
MediaStoreRequestHandler: read MediaStore.Image.ORIENTATION value and…
RobertM-BLS e02b0b9
Merge remote-tracking branch 'RobertM-BLS/mediastore-orientation-fix'
st1hy c000726
When reading exif in MediaStoreRequestHandler, read file directly wit…
st1hy 36ddc4c
Fixed opening input stream without any reason and not closing it. Fo…
st1hy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,17 +20,19 @@ | |
import android.database.Cursor; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.media.ExifInterface; | ||
import android.net.Uri; | ||
import android.provider.MediaStore; | ||
|
||
import java.io.IOException; | ||
|
||
import static android.content.ContentResolver.SCHEME_CONTENT; | ||
import static android.content.ContentUris.parseId; | ||
import static android.provider.MediaStore.Images; | ||
import static android.provider.MediaStore.Video; | ||
import static android.provider.MediaStore.Images.Thumbnails.FULL_SCREEN_KIND; | ||
import static android.provider.MediaStore.Images.Thumbnails.MICRO_KIND; | ||
import static android.provider.MediaStore.Images.Thumbnails.MINI_KIND; | ||
import static android.provider.MediaStore.Video; | ||
import static com.squareup.picasso.MediaStoreRequestHandler.PicassoKind.FULL; | ||
import static com.squareup.picasso.MediaStoreRequestHandler.PicassoKind.MICRO; | ||
import static com.squareup.picasso.MediaStoreRequestHandler.PicassoKind.MINI; | ||
|
@@ -40,6 +42,9 @@ class MediaStoreRequestHandler extends ContentStreamRequestHandler { | |
private static final String[] CONTENT_ORIENTATION = new String[] { | ||
Images.ImageColumns.ORIENTATION | ||
}; | ||
private static final String[] CONTENT_DATA = new String[] { | ||
Images.ImageColumns.DATA | ||
}; | ||
|
||
MediaStoreRequestHandler(Context context) { | ||
super(context); | ||
|
@@ -102,16 +107,58 @@ static PicassoKind getPicassoKind(int targetWidth, int targetHeight) { | |
} | ||
|
||
static int getExifOrientation(ContentResolver contentResolver, Uri uri) { | ||
int exifOrientation = getExitOrientationFromFile(contentResolver, uri); | ||
if (exifOrientation == ExifInterface.ORIENTATION_UNDEFINED) { | ||
exifOrientation = getExifOrientationFromContentResolver(contentResolver, uri); | ||
} | ||
return exifOrientation; | ||
} | ||
|
||
static int getExitOrientationFromFile(ContentResolver contentResolver, Uri uri) { | ||
Cursor cursor = null; | ||
try { | ||
contentResolver.openInputStream(uri); | ||
cursor = contentResolver.query(uri, CONTENT_DATA, null, null, null); | ||
if (cursor == null || !cursor.moveToFirst()) { | ||
return 0; | ||
} | ||
String filePath = cursor.getString(0); | ||
ExifInterface exifInterface = new ExifInterface(filePath); | ||
return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, | ||
ExifInterface.ORIENTATION_UNDEFINED); | ||
|
||
} catch (Exception ignored) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of catching and ignoring all exceptions, I think the proper way would be to retrieve the mime type of the image and only attempt to get orientation from exif metadata if the type supports exif (jpeg), otherwise return undefined. |
||
// In case of error during reading exif, assume no rotation. | ||
return ExifInterface.ORIENTATION_UNDEFINED; | ||
} finally { | ||
if (cursor != null) { | ||
cursor.close(); | ||
} | ||
} | ||
} | ||
|
||
static int getExifOrientationFromContentResolver(ContentResolver contentResolver, Uri uri) { | ||
Cursor cursor = null; | ||
try { | ||
cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null); | ||
if (cursor == null || !cursor.moveToFirst()) { | ||
return 0; | ||
} | ||
return cursor.getInt(0); | ||
|
||
int rotation = cursor.getInt(0); | ||
switch (rotation) { | ||
case 90: | ||
return ExifInterface.ORIENTATION_ROTATE_90; | ||
case 180: | ||
return ExifInterface.ORIENTATION_ROTATE_180; | ||
case 270: | ||
return ExifInterface.ORIENTATION_ROTATE_270; | ||
default: | ||
return ExifInterface.ORIENTATION_NORMAL; | ||
} | ||
} catch (RuntimeException ignored) { | ||
// If the orientation column doesn't exist, assume no rotation. | ||
return 0; | ||
return ExifInterface.ORIENTATION_UNDEFINED; | ||
} finally { | ||
if (cursor != null) { | ||
cursor.close(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exif*. I think it would be more clear to parse the file path and pass it in here instead of a uri. It seems incorrect to call this "getExifOrientationFromFile" and not pass a file.