0

I am building an andorid application that has 8 buttons and I have use very small sized png images for the backgrounds. The size of all images is only 355 kb but when I run the application it gives me error saying

java.lang.OutOfMemoryError: Failed to allocate a 256000012 byte allocation with 16777216 free bytes and 119MB until OOM

Why does it takes so much memory? Is there any better way to use less memory? This is how I made the button in my xml. Thank You

           <Button
            android:id="@+id/groupmeButton"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentLeft="true"
            android:layout_gravity="center"
            android:layout_marginLeft="50dp"
            android:background="@drawable/image1"
            android:onClick="onClick"
            />
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
bibek
  • 1
  • 1

2 Answers2

1

First, image1 is too large in terms of resolution (pixels wide, pixels high).

Second, you probably put image1 in res/drawable/. res/drawable/ is a synonym for res/drawable-mdpi/, meaning that drawables in res/drawable/ are designed for mdpi screens. If you run your app on a higher-density screen, Android will upscale the image to adapt to the higher density. Instead of putting drawables like PNGs and JPEGs in res/drawable/, either:

  • Have different versions of the same image for different densities. The Image Asset wizard in Android Studio can set this up for you.

  • Or, put the image in res/drawable-anydpi/, to tell Android not to upscale or downscale the image.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can you vector Drawable now as Android is supporting Vectors as well. To support vector below API level 21 you can follow below official link.

https://developer.android.com/studio/write/vector-asset-studio.html

Probably that can help you to avoid huge memory for images.

Nikunj Peerbits
  • 785
  • 4
  • 12