1

I have a code in which I call the gallery to select an image that i show in a imageview . This works fine and maintains the aspect ratio. The problem is when the image size is larger than 2048 x 2048 I am using this code when it is the case:

uriIsNowAbitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);

              //...

          int height = uriIsNowAbitmap.getHeight();
          int width = uriIsNowAbitmap.getWidth();

          if ((width>=2048)||(height>=2048)) {

              int newheight  = height/10; // height in pixels
              int newwidth = width/10; // width in pixels    
              Bitmap avatarScaled = Bitmap.createScaledBitmap(uriIsNowAbitmap,  newwidth, newheight, true);

              previewNewAvatar.setImageBitmap(avatarScaled);

}

also works correctly, but with a problem. The images are not of the type shown rotated landscape

to try to explain, I put this example. This is the picture of the gallery:

enter image description here

when I select the picture and assign it to the imageview is shown rotated:

enter image description here

I do not understand why. I've tried a thousand ways and have read a lot of information about resize, i have read threads and sample code for this website (Android: high quality image resizing / scaling) and and many more... but nothing helps. The intention is to show in this way ...make it with photoshop :)

enter image description here

I appreciate any help, took many hours trying to fix it

Regards

Sergio76
  • 3,835
  • 16
  • 61
  • 88

1 Answers1

0

Firstly, you will have to detect the orientation change, and when you detect a change to landscape orientation you just rotate your bitmap accordingly. You will have to declare your Bitmap avatarScaled as a global variable in order to access it in the onConfigurationChanged() method below:

Use the onConfigurationChanged method of Activity to detect change in orientation. See the following code:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(avatarScaled, 0, 0, avatarScaled.getWidth(),
                                     avatarScaled.getHeight(), matrix, true);
previewNewAvatar.setImageBitmap(rotated);
} 
}

You also have to edit the appropriate element in your manifest file to include the android:configChanges Just see the code below:

<activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">
A Random Android Dev
  • 2,691
  • 1
  • 18
  • 17
  • unfortunately the solution you propose is not working properly because the same situation persists. Thank you very much for your interest. Any other ideas? – Sergio76 Aug 19 '12 at 00:18
  • @MiguelC can you post more code please, preferably the entire activity in which this problem arises? – A Random Android Dev Aug 19 '12 at 00:33