facebookarchive / shimmer-android

An easy, flexible way to add a shimmering effect to any view in an Android app.
http://facebook.github.io/shimmer-android/
Other
5.32k stars 694 forks source link

java.lang.OutOfMemoryError · Failed to allocate a.... #34

Closed gastoncesarf closed 6 years ago

gastoncesarf commented 7 years ago

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)

DebdeepG commented 7 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.

ClaudeHangui commented 7 years ago

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

DebdeepG commented 7 years ago

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.

ClaudeHangui commented 7 years ago

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;
    }
xiphirx commented 6 years ago

544c5687cc0f1b2e976cd43e806a5f82e81d6462