Closed gastoncesarf closed 6 years ago
Inside createBitmapAndGcIfNecessary()
, it is not favourable to create bitmap once again when oom is encountered. Instead, using bitmap options, setting inDither
true and using Bitmap.Config.RGB_565
might be useful.
And how will I do that ?? This is what I have so far in my catch block ? BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = true; options.inPreferredConfig = Bitmap.Config.RGB_565;
Now I can only use BitmapFactory to create bitmap but I'm kinda stuck... The createBitmapAndGcIfNecessary() method takes the width and height of the resource but these parameters are not used when creating a bitmap using the BitmapFactory
Please go through the docs link and on passing the inSampleSize, the bitmap will be scaled down by the decoder automatically. Also do take a look at the calculateInSampleSize()
in the example method where you can pass your width and height.
Yes...I did took a look at the link you provided...and I did added the calculateInSampleSize() method..Forgive me, but I still don't see (in the ShimmerLayout class) where are we suppose to take in the resource ..The sample assumes we are loading a bitmap into an Imageview...But our use case here is the Shimmerlayout class.. This is what I've got so far (as per the link you provided) :
private Bitmap createBitmap(int width, int height) {
try {
return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
System.gc();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
544c5687cc0f1b2e976cd43e806a5f82e81d6462
java.lang.OutOfMemoryError: Failed to allocate a 1843212 byte allocation with 686904 free bytes and 670KB until OOM at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java:-2) at android.graphics.Bitmap.nativeCreate(Bitmap.java:-2) at android.graphics.Bitmap.createBitmap(Bitmap.java:975) at android.graphics.Bitmap.createBitmap(Bitmap.java:946) at android.graphics.Bitmap.createBitmap(Bitmap.java:913)