Android : java.lang.OutOfMemoryError:

后端 未结 6 1637
太阳男子
太阳男子 2020-11-27 23:29

I developed an application that uses lots of images on Android.

There are lots of images present in drawable folder say more then 100, I am developing application fo

相关标签:
6条回答
  • 2020-11-27 23:56

    In the above answer there is a getResources() missing in

    img.setImageBitmap(decodeSampleBitmapFromResource(R.drawable.dog_animation, width, height));
    

    so it becomes

    img.setImageBitmap(decodeSampleBitmapFromResource(getResources(), R.drawable.dog_animation, width, height));
    
    0 讨论(0)
  • 2020-11-28 00:00

    Replace:

    img.setBackgroundResource(R.drawable.dog_animation);
    

    By:

    img.setImageBitmap(decodeSampleBitmapFromResource(R.drawable.dog_animation, width, height));
    //dont forget to replace width and heigh by your imageview dimension
    

    An add:

    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 heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
    
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
    
        return inSampleSize;
    }
    

    and

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {
    
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
    
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    

    This is from: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    0 讨论(0)
  • 2020-11-28 00:02

    try stopping your animation at onPause(). there is a big chance its not getting GCED because of that. also optimize ur bitmaps using this site http://tinypng.org, if you don't need the alpha layer, set it to 24bit

    0 讨论(0)
  • 2020-11-28 00:07

    Few days ago, I had the same issue and it generated a similar log. So this is how I went about solving the issue.

    I learned that the pixel of an image being loaded in an ImageView in Android Studio mustn't exceed width 1440 and height 2560.

    The most voted solution on stackoverflow asked is to add the following in our manifest file: android:hardwareAccelerated="false" , android:largeHeap="true"

    just as show in this image As I implemented this, the error wasn't thrown, but this did not work for all devices, it rather only worked for my emulator.

    So, to solve this dilema for my phone, I simply cropped the associated images to the limit pixel after checking the pixel of each image and discarded that was too large for cropping(resizing). Note: for a large number of images you can right a code that checks for the images that exceed the amount of pixel required.

    this is the link to my issue on github.com (allenbangai)

    0 讨论(0)
  • 2020-11-28 00:08
    try {
        //Code here that cause OutOfMemoryError
    } catch (Error ee) {
        ee.printStacktrace();
    }
    
    0 讨论(0)
  • 2020-11-28 00:11

    In your AndroidManifest.xml keep this inside application tag, add largeHeap like this:

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"/>
    
    0 讨论(0)
提交回复
热议问题