Java code
Public Bitmap optimizeBitmap (byte [] source, int maxWidth, int maxHeight ){
Bitmap result = null;
Int length = source. length;
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
Result = BitmapFactory. decodeByteArray (source, 0, length, options );
Int widthRatio = (int) Math. ceil (options. outWidth/maxWidth );
Int heightRatio = (int) Math. ceil (options. outHeight/maxHeight );
If (widthRatio> 1 | heightRatio> 1 ){
If (widthRatio> heightRatio ){
Options. inSampleSize = widthRatio;
} Else {
Options. inSampleSize = heightRatio;
}
}
Options. inJustDecodeBounds = false;
Result = BitmapFactory. decodeByteArray (source, 0, length, options );
Return result;
}
The memory of Android Emulator is only 8 Mb. When you need to display more large charts, it is easy to throw the "bitmap size exceeds VM budget" exception.
BitmapFactory. Options public boolean member variable inJustDecodeBounds. If the value is set to true, the decoder returns NULL. We can query the image without loading the memory.
In the sample code, we use the Options object instance options to obtain the width and height of the image.
The BitmapFactory. Options public int member variable inSampleSize is used to set the image scale-down. For example, when inSampleSize is set to 4, the encoder returns the original 1/4 size image.
Note: When the encoder needs to return an image, set the value of inJustDecodeBounds to false.
Author "Dyingbleed"