Android Imageview Displaying Rotated Images Although Source Is Not Rotated
I am trying to display gallery images in a gridview, and on image click, open a fullscreen view for the image. The weird thing is, that when the images are displaying in the galler
Solution 1:
First you need to create an ExifInterface:
ExifInterfaceexif=newExifInterface(filename);
You can then grab the orientation of the image:
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Here's what the orientation values mean: http://sylvana.net/jpegcrop/exif_orientation.html
So, the most important values are 3, 6 and 8. If the orientation is 6, for example, you can rotate the image like this:
Matrixmatrix=newMatrix();
matrix.postRotate(90);
rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
Solution 2:
Try like this you can avoid OOM Error refer Load Bitmap Effieiently and Whatsapp bitmap loading
publicstaticintcalculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of imagefinalintheight= options.outHeight;
finalintwidth= options.outWidth;
intinSampleSize=1;
if (height > reqHeight || width > reqWidth) {
finalinthalfHeight= height / 2;
finalinthalfWidth= width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and// keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
publicstatic Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensionsfinal BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Check the rotation of the Image and display it properly...as
scaledBitmap=decodeSampledBitmapFromResource(.....);
ExifInterface exif=null;
try {
exif = newExifInterface(filePath);
intorientation= exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrixmatrix=newMatrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} elseif (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} elseif (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
true);
} catch (IOException e) {
e.printStackTrace();
}
Post a Comment for "Android Imageview Displaying Rotated Images Although Source Is Not Rotated"