Details
-
Bug
-
Resolution: Fixed
-
Major
-
0.2
-
None
Description
To reproduce:
when downloading some large images, decoding bitmap may cause OutOfMemoryError.
For 5.98M 4160x3120 JPG , if decoding directly with BitmapFactory, the bitmap need about 4160x3120x4B => 52M memory.
Solution:
1. BitmapFactory options.inSampleSize>1 Decode
// https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
2. Another method is downloading the avatar directly on the filesystem instead of byte array as Thomas commented.