n00b programmer
n00b programmer

Reputation: 2701

change bitmap resolution in Android app

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!

Upvotes: 3

Views: 18471

Answers (2)

n00b programmer
n00b programmer

Reputation: 2701

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

Upvotes: 3

Bhavin
Bhavin

Reputation: 6010

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

Upvotes: 5

Related Questions