I'm facing a curious issue when taking photos in landscape mode. When decoding file to put it in portrait mode using the following code I get the pictures upside down when displaying in a alert dialog:
public static Bitmap decodeFile(String path) {
int orientation;
try {
if(path==null){
return null;
}
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 4;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap bm = BitmapFactory.decodeFile(path,o2);
Bitmap bitmap = bm;
ExifInterface exif = new ExifInterface(path);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
//Log.e("orientation",""+orientation);
Matrix m=new Matrix();
if((orientation==3)){
m.postRotate(180);
m.postScale((float)bm.getWidth(), (float)bm.getHeight());
Log.e("in orientation",""+orientation);
try{
bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
}
catch (Exception ex){
Log.e("Bitmap Size","NULO");
}
return bitmap;
}
else if(orientation==6){
m.postRotate(90);
Log.e("in orientation",""+orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
Log.e("Bitmap Size",String.valueOf(bitmap.getRowBytes() * bitmap.getHeight()));
return bitmap;
}
else if(orientation==8){
m.postRotate(270);
Log.e("in orientation",""+orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
return bitmap;
}
return bitmap;
}
catch (Exception e) {
}
return null;
}
The orientation value when happening this issue is when is 3 value. Apart I had to put a try catch clause because in this case the image didnt't show in the alert dialog.
Does anybody faced this issue?
Regards
I found a solution trying some workarounds. The following code displays the images correctly: