Picture taken in portrait mode rotates

84 Views Asked by At
         Uri selectedImage = outputFileUri;
                     InputStream imageStream =getContentResolver().openInputStream(selectedImage);
                     try
                     {
                         BitmapFactory.Options bounds = new BitmapFactory.Options();
                         bounds.inJustDecodeBounds=true;                                                      BitmapFactory.decodeFile(Common.getRealPathFromURI(selectedImage, AddStoreActivity.this), bounds);
                         BitmapFactory.Options opts = new BitmapFactory.Options();
                         Bitmap bm = BitmapFactory.decodeFile(Common.getRealPathFromURI(selectedImage, AddStoreActivity.this), opts);
                         ExifInterface exif = new ExifInterface(Common.getRealPathFromURI(selectedImage, AddStoreActivity.this));
                         String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
                         int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;   
                         int rotationAngle = 0;
                         if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
                         if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
                         if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
                         Matrix matrix = new Matrix();
                         matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
                         Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
                         bitmap2 = rotatedBitmap;//BitmapFactory.decodeStream(imageStream);
                     }
                     catch(Exception ef)
                     {
                         bitmap2 = BitmapFactory.decodeStream(imageStream);
                     }

Very simple, when in my android app i open camera..and take picture in portrait mode. i get the picture back rotated. how can i solve such issue?the above work i have done had no influence.

1

There are 1 best solutions below

3
On

Try replacing this code

 matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
 Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);

with this

matrix.postRotate(rotationAngle);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);