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,671
  • 7
  • 41
  • 56

2 Answers2

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
  • 6,020
  • 4
  • 24
  • 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,671
  • 7
  • 41
  • 56