-2

I picked an image from gallery and decoded it. Now I just want to resize that bitmap to standard 72x72 size in order to use as an profile photo.

I searched a lot but nothing worked, some of them rotated my image for no reason, some of them makes image very low quality. Is it that hard?

Check scaled 72x72 and original one:

72x72 (As you see, rotated and very bad quality) http://imgim.com/9958incic3494599.png

original: http://imgim.com/3847incix7666386.png

Code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{ 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode)
    { 
    case 100:   // SELECT_PHOTO
        if(resultCode == RESULT_OK)
        {  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream;
            try
            {
                imageStream = getContentResolver().openInputStream(selectedImage);
            }catch (Exception e){ return; }

            Bitmap bm = BitmapFactory.decodeStream(imageStream);
            bm = Bitmap.createScaledBitmap(bm, 72, 72, true);
            UpdateAvatar(bm);
        }
        break;
    }
}
Melih Akalan
  • 57
  • 1
  • 1
  • 8
  • 1
    Wait for answer in your other question?http://stackoverflow.com/questions/20923037/pick-an-image-and-resize-to-72x72 – Magnus Jan 04 '14 at 17:05

1 Answers1

1

Try this function :

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

It helps for me.

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119