0

I'm trying to set a background on my app that will scale well on tablets as well as phones. So I got a png background that is 2560 by 1600 in dimensions but just 44k in size. However everytime I run the app I keep getting this.

08-08 14:50:21.251: E/AndroidRuntime(2924): FATAL EXCEPTION: main
08-08 14:50:21.251: E/AndroidRuntime(2924): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reflap.reflap/com.reflap.reflap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.os.Looper.loop(Looper.java:137)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Method.invoke(Method.java:525)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at dalvik.system.NativeStart.main(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.Activity.setContentView(Activity.java:1895)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.reflap.reflap.MainActivity.onCreate(MainActivity.java:18)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.Activity.performCreate(Activity.java:5133)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-08 14:50:21.251: E/AndroidRuntime(2924):     ... 11 more
08-08 14:50:21.251: E/AndroidRuntime(2924): Caused by: java.lang.reflect.InvocationTargetException
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Constructor.constructNative(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
08-08 14:50:21.251: E/AndroidRuntime(2924):     ... 24 more
08-08 14:50:21.251: E/AndroidRuntime(2924): Caused by: java.lang.OutOfMemoryError
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.content.res.Resources.loadDrawable(Resources.java:2105)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.widget.ImageView.<init>(ImageView.java:127)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.widget.ImageView.<init>(ImageView.java:117)
08-08 14:50:21.251: E/AndroidRuntime(2924):     ... 27 more

I don't understand how it's going out of memory for such a small size.

This is my layout xml for the imageview

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/back" />
Michael Nana
  • 1,969
  • 4
  • 24
  • 36
  • scale down your image using appropriate `Bitmap.decode` methods.http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Raghunandan Aug 08 '13 at 19:00
  • @Raghunandan But I never really call it in the activity class. It's just part of the contentview. – Michael Nana Aug 08 '13 at 19:02
  • your views occupy space in memory. http://www.youtube.com/watch?v=_CruQY55HOk and this http://developer.android.com/training/displaying-bitmaps/manage-memory.html – Raghunandan Aug 08 '13 at 19:03
  • 2
    Your bitmap will, by default, occupy 4 bytes per pixel (32 bits, ARGB) so for 2560x1600 you are trying to use 16MBs of heap! Size of the file means nothing. – Simon Aug 08 '13 at 19:08

3 Answers3

1

Internally, your .png is represented with an integer per pixel (by default). An integer is 4 bytes. If you do the math:

2560 pixels * 1600 pixels * 4 bytes / pixel = 16,384,000 bytes = 15.625 MB

The memory limit per-app on older Android devices is 16 MB. The Portable Network Graphics format is typically compressed, and that's why there is a distinct difference in its file size and the size to represent the image in memory. Also, Android wastes a byte on alpha channel even if your image doesn't have or need an alpha channel.

Samuel
  • 16,923
  • 6
  • 62
  • 75
1

That 44k is compressed size. To use it as a background it is uncompressed into a bitmap. The memory size of a bitmap is the size of the color format for each pixel times the width times the height. Worse, if it is scaled, the system will need more memory to do the scaling as well.

So, for example, if you are using this color format: http://developer.android.com/reference/android/graphics/Bitmap.Config.html#RGB_565

Then the memory required for your bitmap is 2 x 2560 x 1600 = 7.8125 MB.

There is a manifest option for getting more memory: http://developer.android.com/guide/topics/manifest/application-element.html#largeHeap

Although you should also make sure to provide smaller resources for lower DPI devices and to only read in as much data as you need. The BitmapFactory class lets you skip pixels when reading in data if you don't need every pixel. The BitmapRegionDecoder lets you just read in the parts of an image you need.

Lance Nanek
  • 6,377
  • 2
  • 24
  • 19
  • 1
    using largeHeap is most cases not necessary. Also larger the heap more frequent garbabge collection and more frequent pause times – Raghunandan Aug 08 '13 at 19:05
1

Avoid using bitmaps to fill the background. They will not give the best results and will be scaled without preserving screen ratio.

Use 9-patches, shape drawables, simple colors and/or small bitmaps anchored to some corner.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94