I am creating a sort of 'Gallery' app that is displaying all the images in a grid.
The issue is: that some of the images are not displayed in the right orientation.
Here is the code to retrieve the thumbnails
final String[] projection = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID };
//query the thumbnails provider
Cursor thumbnailsCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null,
null, null);
if (thumbnailsCursor.moveToFirst()) {
do {
//get the thumbnail path
fullPath = thumbnailsCursor.getString(fullPathColumnIndex);
thumbnailUri = Uri.parse(fullPath);
//add the uri to the list
thumbnailsList.add(thumbnailUri);
} while (thumbnailsCursor.moveToNext());
thumbnailsCursor.close();
Inside the getView()
of the BaseAdapter
I am using Picasso
image loader library to display the thumbnail, but sometimes the orientation is wrong.
Picasso.with(context).load(new File(photoItem.thumbnail.getPath())).noFade().into(holder.photoImageView);
I have tried querying the real photo data and retrieve the orientation but the process is too slow( couple of seconds long) and the displayed images are too large.
Given the ID of the image you can query the MediaStore to get the path to the original image, and then grab the exif for the orientation from there.
This process is quite fast, and usually only takes a few milliseconds.
Cursor cursor =
CustomApplication
.getContext()
.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Images.Media.DATA}, MediaStore.Images.Media._ID + "=?",
new String[] {"" + PHOTO_ID}, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String fullPath = cursor.getString(0);
cursor.close();
ExifInterface exif = new ExifInterface(fullPath);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
//ROTATE AND FLIP YOUR THUMBNAIL AS NEEDED BASED ON exifOrientation
}
Here is the correct solution. You can query the orientation data from MediaStore so use MediaStore.Images.Media.ORIENTATION to get orientation degreee.
final String[] projection = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Media.ORIENTATION };
Cursor thumbnailsCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (thumbnailsCursor.moveToFirst()) {
do {
int orientationColumnIndex = thumbnailsCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
int orientation = cur.getInt(orientationColumnIndex);
//do what you want to do with orientation value
}while (thumbnailsCursor.moveToNext());
thumbnailsCursor.close();
will give you the orientation in degrees.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With