3

I'm writing an application that uses the phone's camera to take a picture, and then use it in my app. The thing is, the app runs out of memory, and it is probably because of the bitmap's high resolution. Is there a way to keep the bitmap at the same size, but lower the resolution?

Thanks!

n00b programmer
  • 2,651
  • 7
  • 37
  • 54

3 Answers3

6

from jeet.chanchawat' s answer: https://stackoverflow.com/a/10703256/3027225

  public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        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;
    }
Community
  • 1
  • 1
Cüneyt
  • 2,746
  • 26
  • 32
5

You can Set Its Width and Height

Bitmap bm = ShrinkBitmap(imagefile, 150, 150);

Function to Call

Bitmap ShrinkBitmap(String file, int width, int height){

 BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

    if (heightRatio > 1 || widthRatio > 1)
    {
     if (heightRatio > widthRatio)
     {
      bmpFactoryOptions.inSampleSize = heightRatio;
     } else {
      bmpFactoryOptions.inSampleSize = widthRatio; 
     }
    }

    bmpFactoryOptions.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
 return bitmap;
}

}

This are two more links which might Help You. Link 1 & Link 2

Bhavin
  • 5,820
  • 4
  • 22
  • 26
  • this works when I'm using the decodeResource option to get my bitmap. But, when workingfrom the get image intent, I get a Uri and do this: Uri selectedImageUri = data.getData(); Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri); Is there a way to resize after I have the bitmap in this way? – n00b programmer Mar 31 '12 at 20:49
3

this can be done using Options.inSampleSize, when creating the bitmap

n00b programmer
  • 2,651
  • 7
  • 37
  • 54